Java Tutorials
Java 8 introduced Lambda Expressions, These expressions depends on Functional Interfaces. A Functional Interface is an Interface which has only one Abstract method, and also it can have static and default methods. It can optionally be decorated with @FunctionalInterface Annotation.
Earlier java(i.e before JDK8) , an interfaces can be implemented using class/anonymous class. An anonymous class is a class which has no class name, and can implement interface methods.
Functional interfaces can be used as a return type for lambda expressions and method references.
Supplier functional iterfaces, which takes no input amd returns Result. Supplier functional interfaces has one abstract generic method called get
Java Supplier functional interfaces categorized into 2 types, 1. to support primitive types, 2. to support Generic Types
Supplier Interfaces has one abstract method called get
T get()
import java.util.function.* IntSupplier intSup = () -> {return 500;} intSup.getAsInt(); // returns 500 DoubleSupplier dubSup = ()-> {return Math.random();} System.out.println(dubSup.getAsDouble()); 0.4991964608619356 System.out.println(dubSup.getAsDouble()); 0.47916779058164016 System.out.println(dubSup.getAsDouble()); 0.8797546609922758
String file = "Countries.txt"; Supplier<Long> fileSupplier = () ->{ File f = new File(file); return f.length(); } //Calling supplier interface get method fileSupplier.get() // returns file size.
ADS