A function that can be shared or referred to as an object.
Functional Interfaces
Single abstract method interface.
Method References
Uses function as a parameter to invoke a method.
Default method
It provides an implementation of methods within interfaces enabling 'Interface evolution' facilities.
forEach() Method In Iterable Interface
Java.lang interface now supports a “forEach” function. Iterable that can iterate over the collection’s items. The Iterable interface has a default method called “forEach.”
Stream API
Abstract layer that provides pipeline processing of the data.
Date Time API
New improved joda-time inspired APIs to overcome the drawbacks in previous versions
Optional
Wrapper class to check the null values and helps in further processing based on the value.
Nashorn, JavaScript Engine
An improvised version of JavaScript Engine that enables JavaScript executions in Java, to replace Rhino.
Functional interfaces
Functional Interfaces are an interface with only one abstract method (Single Abstract Method (SAM)).
functional -> wraps a function as an interface.
can have any number of default, static, and overridden methods.
For declaring @FunctionalInterface annotation is optional to use. If this annotation is used for interfaces with more than one abstract method, it will generate a compiler error.
cannot extend another interface with abstract methods as it will void the rule of one abstract method per functional interface.
can extend other interfaces which do not have any abstract method and only have the default, static, another class overridden, and normal methods.
default method
A method in the interface that has a predefined body
It uses the keyword default
to have 'Backward Compatibility'. In case a new abstract method is added to the interface, all classes implementing the interface will break and will have to implement the new method. With default methods, there will not be any impact on the interface implementing classes.
can be overridden if needed in the implementation
does not qualify as synchronized or final.
static method
Static methods, which contains method implementation is owned by the interface and is invoked using the name of the interface, it is suitable for defining the utility methods and cannot be overridden.
Example of pre-defined functional interfaces
java.util.function
Some of the famous pre-defined functional interfaces from previous Java versions are Runnable, Callable, Comparator, and Comparable. While Java 8 introduces functional interfaces like Supplier, Consumer, Predicate, etc. Please refer to the java.util.function doc for other predefined functional interfaces and its description introduced in Java 8.
Runnable: use to execute the instances of a class over another thread with no arguments and no return value ( run() ).
Callable: use to execute the instances of a class over another thread with no arguments and it either returns a value or throws an exception.
Comparator: use to sort different objects in a user-defined order
Comparable: use to sort objects in the natural sort order
Categories of pre-defined function interfaces (new in Java 8)
Function: To transform arguments in returnable value.
Predicate: To perform a test and return a Boolean value.
Consumer: Accept arguments but do not return any values.
Supplier: Do not accept any arguments but return a value.
Operator: Perform a reduction type operation that accepts the same input types.
Lambda expression
Type of function without a name
It may or may not have results and parameters
Known as an anonymous function as it does not have type information by itself.
can only be applied to the single abstract method of Functional Interface
? It will infer the return type, type, and several arguments from the signature of the abstract method of functional interface ?
beneficial in iterating, filtering, and extracting data from a collection.
Receives type once it is assigned to a functional interface
Same lambda expression can be assigned to different functional interface types and can have a different type
Predicate<String> stringPredicate = s -> s.isEmpty(); Predicate<List> listPredicate = s -> s.isEmpty(); Function<String, Boolean> func = s -> s.isEmpty(); Consumer<String> stringConsumer = s -> s.isEmpty();
Common ways to use the expression
Assignment to a functional Interface —> Predicate<String> stringPredicate = s -> s.isEmpty(); Can be passed as a parameter that has a functional type —> stream.filter(s -> s.isEmpty()) Returning it from a function —> return s -> s.isEmpty() Casting it to a functional type —> (Predicate<String>) s -> s.isEmpty()
Means that we could create object of Functional interface by simply passing method (static or instance or constructor) of existing class.
Sayable sayable = MethodReference::saySomething;
forEach
It is a default method defined in the Iterable interface. Collection classes which extends Iterable interface can use forEach() method to iterate elements.
This method takes a single parameter which is a functional interface. So, you can pass lambda expression as an argument.
public static void main(String[] args) {
List gamesList = new ArrayList();
gamesList.add("Football");
gamesList.add("Cricket");
gamesList.add("Chess");
gamesList.add("Hocky");
System.out.println("------------Iterating by passing lambda expression--------------");
gamesList.forEach(games -> System.out.println(games));
System.out.println("------------Iterating by passing method reference---------------");
gamesList.forEach(System.out::println);
// forEachOrdered() - iterating using stream
System.out.println("------------Iterating by passing lambda expression---------------");
gamesList.stream().forEachOrdered(games -> System.out.println(games));
System.out.println("------------Iterating by passing method reference---------------");
gamesList.stream().forEachOrdered(System.out::println);
}
Stream API
A new java.util.stream has been added in Java 8 to perform filter/map/reduce like operations with the collection. Stream API will allow sequential as well as parallel execution.
Collection interface has been extended with stream() and parallelStream() default methods t
In Java 8, the “java.util” package included an optional class. The public final class “Optional” is used to handle NullPointerException in a Java program.