Skip to content

Instantly share code, notes, and snippets.

jshell> Optional.of("something").orElse("empty")
$5 ==> "something"
jshell> Optional.empty().orElse("empty")
$6 ==> "empty"
jshell> Optional.empty().ifPresentOrElse(System.out::println, () -> System.out.println("empty"))
empty
jshell> Optional.of("something").ifPresentOrElse(System.out::println, () -> System.out.println("empty"))
something
jshell> Optional.empty().ifPresent(System.out::println)
jshell> Optional.of("something").ifPresent(System.out::println)
something
jshell> Optional.of("something").
equals( filter( flatMap( get() getClass()
hashCode() ifPresent( ifPresentOrElse( isPresent() map(
notify() notifyAll() or( orElse( orElseGet(
orElseThrow( stream() toString() wait(
jshell> Optional.empty().isPresent()
$3 ==> false
jshell> Optional.of("something").isPresent()
$4 ==> true
jshell> Optional.of("something")
$2 ==> Optional[something]
jshell> Optional.empty()
$1 ==> Optional.empty
jshell> entries.collect(Collectors.groupingBy(e -> e.getKey(), Collectors.flatMapping(e -> e.getValue().stream(), Collectors.toSet())))
$2 ==> {1=[a, b, c], 2=[d]}
jshell> entries.collect(Collectors.groupingBy(e -> e.getKey(), Collectors.mapping(e -> e.getValue(), Collectors.toSet())))
$1 ==> {1=[[b, a], [c, a]], 2=[[d]]}
jshell> Stream<Map.Entry<Integer,Set<String>>> entries = Stream.of(Map.entry(1, Set.of("a", "b")), Map.entry(1, Set.of("a", "c")), Map.entry(2, Set.of("d")))
entries ==> java.util.stream.ReferencePipeline$Head@69379752