Attention: Java promotion battalion, the first time the latest articles are delivered, 10T free learning materials are always available!!!
Functional Interfaces concepts
A functional interface is an interface that contains only one abstract method.They can only do one thing.Starting with Java 8, lambda expressions Instances that can be used to represent functional interfacets.Function interfacets can have multiple default or static methods.Runnable, ActionListener, and Comparable are examples of functional interfacets.
Before Java 8, we had to create anonymous internal class objects or implement these interfaces.
<!-- more -->
// Java program to demonstrate functional interface class Test { public static void main(String args[]) { // create anonymous inner class object new Thread(new Runnable() { @Override public void run() { System.out.println("New thread created"); } }).start(); } }
Output:
New thread created
Starting with Java 8, we can use lambda expressions To represent an instance of a functional interface, as follows:
// 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(); } }
Output:
New thread created
@FunctionalInterface annotation
The interface labeled with the @FunctionalInterface annotation ensures that there cannot be more than one abstract method.If there are multiple Abstract methods, the compiler will report an "Unexpected @FunctionalInterface annotation" error.However, this annotation is not mandatory.
// Java program to demonstrate lamda expressions to implement // a user defined functional interface. @FunctionalInterface interface Square { int calculate(int x); } class Test { public static void main(String args[]) { int a = 5; // lambda expression to define the calculate method Square s = (int x)->x*x; // parameter passed and return type must be // same as defined in the prototype int ans = s.calculate(a); System.out.println(ans); } }
Output:
25
java.util.function package
The java.util.function package in Java 8 contains many built-in functional interface s, such as -
- Predicate: The Predicate interface has an abstract method, test, that returns a Boolean value.
public interface Predicate { public boolean test(T t); }
- The BinaryOperator: BinaryOperator interface has an abstract method apply that passes in two parameters and returns the same type of result.
public interface BinaryOperator { public T apply(T x, T y); }
- The Function:Function interface has an abstract method apply that passes in a parameter of type T and returns a result of type R.
public interface Function { public R apply(T t); }
// A simple program to demonstrate the use // of predicate interface import java.util.*; import java.util.function.Predicate; class Test { public static void main(String args[]) { // create a list of strings List<String> names = Arrays.asList("Geek","GeeksQuiz","g1","QA","Geek2"); // declare the predicate type as string and use // lambda expression to create object Predicate<String> p = (s)->s.startsWith("G"); // Iterate through the list for (String st:names) { // call the test method if (p.test(st)) System.out.println(st); } } }
Output:
Geek GeeksQuiz Geek2
summary
- Function interface has only one abstract method, but it can have multiple default or static methods.
- The @FunctionalInterface annotation is used to ensure that an interface cannot have multiple Abstract methods.The use of this comment is optional.
- The java.util.function package contains many built-in functional interface s in Java 8.