Supplier
The Supplier functional interface is also a type of functional interface that does not take any input or argument and yet returns a single output. This type of functional interface is generally used in the lazy generation of values. Supplier functional interfaces are also used for defining the logic for the generation of any sequence. For example – The logic behind the Fibonacci Series can be generated with the help of the Stream. generate method, which is implemented by the Supplier functional Interface.
The different extensions of the Supplier functional interface hold many other suppliers functions like BooleanSupplier, DoubleSupplier, LongSupplier, and IntSupplier. The return type of all these further specializations is their corresponding primitives only.
Syntax / Prototype of Supplier Functional Interface is –
@FunctionalInterface public interface Supplier<T>{ // gets a result …………. // returns the specific result ………… T.get(); }
Below is the implementation of the above topic:
Java
// 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); } } } |
Geek GeeksQuiz Geek2
Important Points/Observations:
Here are some significant points regarding Functional interfaces in Java:
- In functional interfaces, there is only one abstract method supported. If the annotation of a functional interface, i.e., @FunctionalInterface is not implemented or written with a function interface, more than one abstract method can be declared inside it. However, in this situation with more than one functions, that interface will not be called a functional interface. It is called a non-functional interface.
- There is no such need for the @FunctionalInterface annotation as it is voluntary only. This is written because it helps in checking the compiler level. Besides this, it is optional.
- An infinite number of methods (whether static or default) can be added to the functional interface. In simple words, there is no limit to a functional interface containing static and default methods.
- Overriding methods from the parent class do not violate the rules of a functional interface in Java.
- The java.util.function package contains many built-in functional interfaces in Java 8.
Functional Interfaces in Java
Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some of the primitive data types and primitive methods for integrity and simplicity. There are no solely functions present in a programming language called Java. Functions in the Java programming language are part of a class, and if someone wants to use them, they have to use the class or object of the class to call any function.