package com.nisum.java9Features.StreamApi; import java.util.*; import java.util.stream.*; public class TestOfNullable { public static void main(String[] args) { List l = new ArrayList(); l.add("A"); l.add("B"); l.add(null); l.add("C"); l.add("D"); l.add(null); System.out.println(l); List l2 = l.stream().filter(o -> o != null).collect(Collectors.toList()); System.out.println(l2); List l3 = l.stream().flatMap(o -> Stream.ofNullable(o)).collect(Collectors.toList()); System.out.println(l3); Map m = new HashMap<>(); m.put("A", "Apple"); m.put("B", "Banana"); m.put("C", null); m.put("D", "Dog"); m.put("E", null); List l4 = m.entrySet().stream().map(e -> e.getKey()).collect(Collectors.toList()); System.out.println(l4); List l5 = m.entrySet().stream().flatMap(e -> Stream.ofNullable(e.getValue())).collect(Collectors.toList()); System.out.println(l5); } }