This is a blog related to the Java 8 new features introduced as part of the functional programming approach.
Java 8 Streams |
---|
Streams are equivalent of the sequence from the functional programming language. Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels. |
What are the features of Streams? |
---|
Stream provides following features:
|
Convert List into map using streams API |
---|
// Converting Product List into a Map Map productPriceMap = productsList.stream() .collect(Collectors.toMap(p->p.id, p->p.name)); |
Method reference by streams? |
---|
List productPriceList = productsList.stream() .filter(p -> p.price > 30000) // filtering data .map(Product::getPrice) // fetching price by referring getPrice method .collect(Collectors.toList()); // collecting as list |
What are the ways to generate Streams in Java? |
---|
There are two ways to generate streams in java. With Java 8, Collection interface has two methods to generate a Stream.
|
Comaprator in Lamda ways |
---|
1. Classic
2. Lambda expression equivalent.
|