From Java 9 onwards, we can declare private methods in interface. In this post I will show with an example of how to do it. Private methods in interface are useful if you have logic shared by more than one default interface methods. We can put the shared logic in the private method. Below is…… Continue reading Private Interface Methods
Category: Core
BiFunction Example
In this post, I will show how to use BiFunction functional interface with an example. BiFunction functional interface is similar to Function functional interface. It takes an input and produces an output. The difference lies in the number of inputs taken by the BiFunction functional interface. Function functional interface takes one input and produced one…… Continue reading BiFunction Example
Combining two or more Function functional interface
In this post, I will explain how to combine Function functional interfaces. They are two ways in which we can combine Function functional interface and it is achieved using two methods in Function interface as mentioned below 1) default Function andThen(Function after) 2) default Function compose(Function before) Both the methods accept an implementation of Function…… Continue reading Combining two or more Function functional interface
Function functional interface
In this post, with an example I will show how to use Function functional interface. Function interface is a functional interface that takes an input and produces an output. This is achieved by its “apply” method. Below is an example Main Code 1 package function; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.function.Function;…… Continue reading Function functional interface
Sorting a section of an array
In this post I will explain how to sort a section of an array. The java Arrays class provides a static method named “sort” which takes the below three arguments 1) the array 2) fromIndex 3) toIndex This method sorts a section of the array. The section to be sorted is indicated by fromIndex (position…… Continue reading Sorting a section of an array
Combining two or more Consumer Functional Interface
In this post I will explain how to combine two or more Consumer Functional interface implementations. Consumer Functional interface has a default method “andThen”, which combines two Consumer Functional interface implementations. The method signature is as shown below default Consumer andThen(Consumer after) The way two Consumer implementations are combined using “andThen” method is as shown…… Continue reading Combining two or more Consumer Functional Interface
Sorting arrays using Comparator
This post explains how to sort arrays using comparator with an example. The java jdk provides Arrays class, which has a static method named “sort”. This method takes two arguments 1) the array to be sorted 2) Comparator which contains the logic to sort the array Below is the example which sort cards based on…… Continue reading Sorting arrays using Comparator
Creating array dynamically
In this post I will explain how to create array dynamically with an example. This is useful when we don’t know the type of array until runtime. Main code 1 package reflection; 2 3 import java.lang.reflect.Array; 4 5 public class ArraysDemo { 6 public static void main(String[] args) { 7 //Creating single dimension array 8…… Continue reading Creating array dynamically
Creating a temporary file and directory
The post shows you how to create a temporary file and directory in java with an example. Main Code 1 package nio; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 8 public class Example1 { 9 public static void main(String[] args) throws IOException { 10 Path path = Files.createTempFile(“prefix”,…… Continue reading Creating a temporary file and directory
Supplier Functional Interface Example
Java 8 added a new functional interface named “Supplier”. The purpose of this interface is to supply instances. I will be explaining using the below example. In the example I have want of list of employees. I will separate the logic behind employee instance creation and adding to list. The employee instance will be created…… Continue reading Supplier Functional Interface Example