12 -Java 8-11
12 -Java 8-11
8 - 11
ZoneId id = ZoneId.of("Europe/Paris");
System.out.println("ZoneId: " + id);
Example:
Stream<String> streamOfArray = Stream.of("a","b","c");
8. forEach() Method
● 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.
8. forEach() Method (Cont.)
Example:
public static void main(String[] args) {
List<String> gamesList = new ArrayList<String>();
gamesList.add("Football");
gamesList.add("Cricket");
gamesList.add("Chess");
gamesList.add("Hocky");
gamesList.forEach(games ->
System.out.println(games));
}
9. Optional class
● Java introduced a new class Optional in jdk8.
● It provide a better alternate mechanism for a method to
indicate no result to the caller.
● It is a public final class and used to deal with
NullPointerException in Java application.
9. Optional class (Cont.)
Example:
public static void main(String[] args) {
String[] words = new String[10];
Optional<String> checkNull =
Optional.ofNullable(words[5]);
if (checkNull.isPresent()) {
String word = words[5].toLowerCase();
System.out.print(word);
}
else
System.out.println("word is null");
}
THANKS
!