Not Only Lambdas, but it includes lot more new features
- Lambda expressions
- Method references
- Default Methods (Defender methods)
- A new Stream API.
- A new Date/Time API.
Let's look at some of features that we are excited to share with you:
The biggest feature of Java 8 is its support to lambda expressions.
Lambda expressions are anonymous methods, aimed at mainly addressing the “vertical problem” by replacing the machinery of anonymous inner classes with a lighter-weight mechanism in applicable cases.
As Specified by JSR 335: It defines the syntax of Lambda Expressions, including rules for the new kinds of parameters lists and statement lists.
Lambda Expressions are not exactly used to remove Anonymous classes. Lambdas anonymous functions used to decrease the overhead of Anonymous classes.
Lambda expressions in Java is usual written using syntax
(argument) -> (body).
Some more examples:
There are four kinds of method references:
|Reference to a static method|
|Reference to an instance method of a particular object|
|Reference to an instance method of an arbitrary object of a particular type|
|Reference to a constructor|
A non-constructor method reference consists of a qualifier, followed by the :: symbol, followed by an identifier. The qualifier is either a type or an expression, and the identifier is the referenced method's name. The qualifier is a type for static methods, whereas the qualifier is a type or expression for non-static methods. If the qualifier is an expression, the expression is the object on which the method is invoked.
A constructor reference consists of a qualifier, followed by the :: symbol, followed by the keyword new. The qualifier is always a type, and the new keyword is the name of the referenced constructor. The qualifier type must support the creation of instances; for example, it cannot be the name of an abstract class or interface.
Before a non-static method can be invoked, an object on which to invoke this method is required. This target object is known as a receiver. The receiver can be provided as an expression (a bound non-static method) or a type (an unbound non-static method). I'll have more to say about receivers in these contexts later in this article.
Default Methods(Defender Methods) :
A new addition to the Java language, you can now add method bodies to interfaces (called default methods). These methods are implicitly added to every class which implements the interface.
New Stream API:Stream API enables you the power of parallel processing. You can find the Stream interface in java.util.stream package .
The most obvious way to create a stream is from a
The Collection interface has two default methods on it for creating streams:
- stream(): Returns a sequential Stream with the collection as its source.
- parallelStream(): Returns a possibly parallel Stream with the collection as its source.
The ordering of the Stream relies on the underlying collection just like an Iterator.