Sunday, March 25, 2018

JShell and Optional

jshell> List<String> people = List.of("Bill", "Sue", "Jenny", "Sarah", "Tim", "Brian");
people ==> [Bill, Sue, Jenny, Sarah, Tim, Brian]

jshell> people.stream().filter(p -> p.substring(0,1).equals("B"))
    .forEach(System.out::println) Bill Brian jshell> List<String> results = people.stream(). ...> filter(p -> p.substring(0,1).equals("B")). ...> collect(Collectors.toList()) results ==> [Bill, Brian] jshell> Optional<String> s = people.stream(). ...> filter(p -> p.startsWith("T")). ...> findFirst() s ==> Optional[Tim] jshell> Optional<String> s = people.stream(). ...> filter(p -> p.startsWith("W")). ...> findFirst() s ==> Optional.empty jshell> System.out.println(s.orElse("not found")) not found jshell>