Is Stream.count() guranteed to visit each element?
问题 In other words, is the following line guranteed to print num lines? int num = list.stream().peek(System.out::println).count(); This question was triggered by a discussion in the comments of https://stackoverflow.com/a/41346586/2513200 I vaguely remember a discussion that optimizations that avoid iteration might be legal, but didn't find anything conclusive during a quick search. The JavaDocs for Stream.count contain this statement: This is a special case of a reduction and is equivalent to: