Java 8 Handy Notes
Java Method References – Method reference is
used to refer method of functional interface or class. It is compact and easy
form of lambda expression.
There are 3-types of method references in java
1.
Reference to a static
method
2.
Reference to a instance
method
3.
Reference to a constructor
Syntax for above each type of method
references.
1.
Syntax of static
Method Reference
<Class name>
:: <static method name>
2.
Syntax of instance
Method Reference
<Class name> :: <instance method name>
3.
Syntax of Constructor
Method Reference
<Class
name> :: <new> (it is new keyword)
Functional Interface - any interface that has
only one abstract method is called functional interface, @FunctionalInterface
annotation can be used to declare an interface as functional interface (optional).
-
It can have any number of
static and default methods
-
It should have only one
abstract method
-
It is also known as Single
Abstract Method Interface (SAM), it is a new feature to achieve functional
programming approach
-
A functional interface can
extends another interface only when it does not have any abstract method
-
Some of the java
pre-defined functional interfaces are
Consumer<T>, Function<T, R>,
Predicate<T> ….. Etc.
Streams – Java provides a new additional
package in java 8 called java.util.stream, to play or perform any operation on
lists, we will convert list into stream and perform operations.
Operations performed on stream are divided into 2-categories
1.
Intermediate operations
(e.g., filter, map, sorted ….)
2.
Terminal operations
(eg., collect, forEach, reduce, …)
Comments
Post a Comment