Java 1.
Java 8 Programming Language Enhancements
Java 8 provides following features for Java Programming:
o Lambda expressions,
o Method references,
o Functional interfaces,
o Stream API,
o Default methods,
o Base64 Encode Decode,
o Static methods in interface,
o Optional class,
o Collectors class,
o ForEach() method,
o Parallel array sorting,
o Nashorn JavaScript Engine,
o Parallel Array Sorting,
o Type and Repating Annotations,
o IO Enhancements,
o Concurrency Enhancements,
o JDBC Enhancements etc.
default methods: Java provides a facility to create default methods inside the interface. Methods which are defined
inside the interface and tagged with default are known as default methods. These methods are non-abstract methods.
=================================================
Java forEach loop
Java provides a new method forEach() to iterate the elements. It is defined in Iterable and
Stream interface. It is a default method defined in the Iterable interface. Collection classes
which extends Iterable interface can use forEach loop to iterate elements.
This method takes a single parameter which is a functional interface. So, you can pass
lambda expression as an argument.
forEach() Signature in Iterable Interface
default void forEach(Consumer<super T>action)
Java Stream forEachOrdered() Method
Along with forEach() method, Java provides one more method forEachOrdered(). It is used to
iterate elements in the order specified by the stream.
Singnature: void forEachOrdered(Consumer<? super T> action)
import [Link];
import [Link];
public class ForEachLoop {
public static void main(String[] args) {
List<String> gamesList = new ArrayList<String>();
[Link]("Football");
[Link]("Cricket");
[Link]("Chess");
[Link]("Hocky");
[Link]("------------Iterating by Lamda Expressin---------------");
[Link](games -> [Link](games));
[Link]("------------Iterating by passing method
reference---------------");
[Link]([Link]::println);
[Link]("------------Iterating by passing method
reference---------------");
[Link]().forEach([Link]::println);
[Link]("------------Iterating by Lamda Expressin---------------");
[Link]().forEachOrdered(game->[Link](game));
}
}
flatMap() V/s map() :
1) map() takes a Stream and transform it to another Stream. It applies a function on each element of
Stream and store return value into new Stream. It does not flatten the stream. But flatMap() is the
combination of a map and a flat operation i.e, it applies a function to elements as well as flatten them.
2) map() is used for transformation only, but flatMap() is used for both transformation and flattening.
class GFG
{
// Driver code
public static void main(String[] args)
{
// Creating a list of Prime Numbers
List<Integer> PrimeNumbers = [Link](5, 7, 11,13);
// Creating a list of Odd Numbers
List<Integer> OddNumbers = [Link](1, 3, 5);
// Creating a list of Even Numbers
List<Integer> EvenNumbers = [Link](2, 4, 6, 8);
List<List<Integer>> listOfListofInts =
[Link](PrimeNumbers, OddNumbers, EvenNumbers);
[Link]("The Structure before flattening is : " +
listOfListofInts);
// Using flatMap for transformating and flattening.
List<Integer> listofInts = [Link]()
.flatMap(list -> [Link]())
.collect([Link]());
[Link]("The Structure after flattening is : " +
listofInts);
}
}