Hello guys, functional interface in Java are an important concept but not many developer pay enough attention to them. They learn lambda and method reference but the functional interface from java.util.function package. While its not really possible to learn all the functional interfaces on that package but you can learn a few more commonly used ones like Predicate, Supplier, Consumer etc and that's what you will learn in this article. But, before we get to the top 5 functional interfaces that every Java developer should learn, let me tell you a bit about what Java really is.
For those of you who don't know, Java is basically a general-purpose, class-based, object-oriented programming language that aims to have lesser implementation dependencies. You can also think of Java as a computing platform for application development.
What sets Java apart is that it is fast, secure, and reliable. It can be used for developing Java applications with the help of laptops, data centers, game consoles, scientific supercomputers, and even cell phones.
5 Essential Functional Interfaces Every Java Developer Should Learn
A Java platform is basically a collection of programs that will help you develop and run Java programming applications. It is made up of an execution engine, a compiler, and a set of libraries. It is basically a set of computer software and specifications.
Java was developed by James Gosling when he was working at Sun Microsystems. It was later acquired by the Oracle Corporation.
Java can also be used for developing android applications, creating Enterprise Software, creating mobile java applications, creating scientific computing applications, Big Data analytics, programming hardware devices, and server-side technologies like Apache and GlassFish.
1. The Supplier Interface
import java.util.function.Supplier;
public class Main {
public static void main(String args[])
{
// This function returns a random value.
Supplier<Double> randomValue = () -> Math.random();
// Print the random value using get()
System.out.println(randomValue.get());
}
}
0.5685808855697841
2. Consumer Interface
// Java Program to demonstrate
// Consumer's accept() method
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[])
{
// Consumer to display a number
Consumer<Integer> display = a -> System.out.println(a);
// Implement display using accept()
display.accept(10);
// Consumer to multiply 2 to every integer of a list
Consumer<List<Integer> > modify = list ->
{
for (int i = 0; i < list.size(); i++)
list.set(i, 2 * list.get(i));
};
// Consumer to display a list of numbers
Consumer<List<Integer> >
dispList = list -> list.stream()
.forEach(a -> System.out.print(a + " "));
List<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(1);
list.add(3);
// Implement modify using accept()
modify.accept(list);
// Implement dispList using accept()
dispList.accept(list);
}
}
3. Function
// Java program to demonstrate Implementation of
// functional interface using lambda expressions
class Test {
public static void main(String args[])
{
// lambda expression to create the object
new Thread(() -> {
System.out.println("New thread created");
}).start();
}
}
4. Predicate
5. BiFunction
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
public class Java8BiFunctionDemo {
public static void main(String[] args) {
// takes two Integers and return an Integer
BiFunction<Integer, Integer, Integer> func = (x1, x2) -> x1 + x2;
Integer result = func.apply(2, 3);
System.out.println(result); // 5
// take two Integers and return an Double
BiFunction<Integer, Integer, Double> func2 = (x1, x2) -> Math.pow(x1, x2);
Double result2 = func2.apply(2, 4);
System.out.println(result2); // 16.0
// take two Integers and return a List<Integer>
BiFunction<Integer, Integer, List<Integer>> func3
= (x1, x2) -> Arrays.asList(x1 + x2);
List<Integer> result3 = func3.apply(2, 3);
System.out.println(result3);
}
}
5
16.0
[5]
good
ReplyDelete