Engineering Full Stack Apps with Java and JavaScript
Java 8 has has made some improvements in collections framework such as a new forEach loop, better type inference etc. We will list out those features here.
The default forEach method of the Iterable interface performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
Signature of the methiod is: void forEach(Consumer<? super T> action)
Example
Without forEach using for-each loop:
for(String s: stringList){
System.out.println(s);
}
Without forEach using Iterator:
Iterator<String> i = stringList.iterator();
While(i.hasNext()){
System.out.println(i.next)
}
With forEach
stringList.forEach(s -> System.out.println(s));
Java 8 type inference has been improved.
We can now pass 'new HashMap<>()' into a method that accepts 'Hashmap <String,String>'
EXAMPLE-TO-BE-ADDED
The package java.util.stream contains classes to support functional-style operations on streams of elements, such as map-reduce transformations on collections. We have a dedicated section of notes for Streams.