Sometimes it would be handy do "something" (e.g. print) with every element in a stream in between steps of processing the stream, e.g. for debugging.
A simple example could look like this, unfortunately this does not work as forEach
consumes the stream:
List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
List<String> filteredList =
list.stream()
.filter(s -> s.startsWith("t"))
.forEach(System.out::println)
.collect(Collectors.toList());
How can this be achieved?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…