June 2018
Beginner
722 pages
18h 47m
English
A Stream<T> peek(Consumer<T> action) intermediate operation applies the provided Consumer function to each stream element and does not change this Stream (returns the same element value it has received) because the Consumer function returns void and cannot affect the value. This operation is used for debugging.
The following code shows how it works:
List<String> list = List.of("1", "2", "3", "4", "5");list.stream().peek(s-> { if("3".equals(s)){ System.out.print(3); }}).forEach(System.out::print); //prints: 123345
Read now
Unlock full access