Java 8 has brought in a very different style of programming which is significantly different from its previous versions. Here we will discuss about some of the important features being introduced in Java 8.
- Lambda Expression
- Streams & Collections
- Functional Interface
- Default & Static Methods in Interface
- Optional Class
- Date/Time API Changes
Lambda Expression: Lambda Expressions in java 8 supports functional programming . Its like an anonymous function where in we don’t need to declare method name or parameter type. This can be explained with an example below:
Basic Syntax () -> System.out.println("Hi I am Lamda Expression");
Example@FunctionalInterface
public interface DemoFunctionalInterface {
public void addNumbers(int a, int b);
}
class Test {
public static void main(String args[]) {
DemoFunctionalInterface
obj = (x,y)-> x+y;
obj.addNumbers(5,8);
}
}
Above example demonstrate that lambda expression can only be used to implement functional interfaces. The code which we provide int the right side of lambda expression is the implementation of abstract method of functional interface.
Streams & Collections: This is another very important feature of java 8 which basically helps to process the collections(map, set, list etc) by applying different operations which can be either intermediate or terminal operations. The stream API significantly reduces the amount of code by as number of operations can be pipelined together to get the desired result.
Different Operations supported by stream API are :
- Filter
- Map
- Collect
- ForEach
- Sorted
- Reduce