Why filter() after flatMap() is “not completely” lazy in Java streams?
问题 I have the following sample code: System.out.println( "Result: " + Stream.of(1, 2, 3) .filter(i -> { System.out.println(i); return true; }) .findFirst() .get() ); System.out.println("-----------"); System.out.println( "Result: " + Stream.of(1, 2, 3) .flatMap(i -> Stream.of(i - 1, i, i + 1)) .flatMap(i -> Stream.of(i - 1, i, i + 1)) .filter(i -> { System.out.println(i); return true; }) .findFirst() .get() ); The output is as follows: 1 Result: 1 ----------- -1 0 1 0 1 2 1 2 3 Result: -1 From