- 
    Bug 
- 
    Resolution: Won't Fix
- 
     P4 P4
- 
    None
- 
    11.0.9.1, 16, 17
                    Following up on https://bugs.openjdk.java.net/browse/JDK-8075939, there's still a case where Stream.flatMap() doesn't get short circuited, when using Stream.iterator():
// --------------------------------------------------------
Iterator<Integer> it =
Stream.of("a", "b")
.flatMap(s -> Stream
.of(1, 2, 3, 4)
.filter(i -> { System.out.println(i); return true; }))
.iterator();
it.hasNext(); // This consumes the entire flatmapped stream
it.next();
// --------------------------------------------------------
The above program prints:
1
2
3
4
This program never stops:
// --------------------------------------------------------
Iterator<Integer> it =
Stream.of("a", "b")
.flatMap(s -> Stream
.iterate(1, i -> i)
.filter(i -> { System.out.println(i); return true; }))
.iterator();
it.hasNext();
it.next();
// --------------------------------------------------------
            
// --------------------------------------------------------
Iterator<Integer> it =
Stream.of("a", "b")
.flatMap(s -> Stream
.of(1, 2, 3, 4)
.filter(i -> { System.out.println(i); return true; }))
.iterator();
it.hasNext(); // This consumes the entire flatmapped stream
it.next();
// --------------------------------------------------------
The above program prints:
1
2
3
4
This program never stops:
// --------------------------------------------------------
Iterator<Integer> it =
Stream.of("a", "b")
.flatMap(s -> Stream
.iterate(1, i -> i)
.filter(i -> { System.out.println(i); return true; }))
.iterator();
it.hasNext();
it.next();
// --------------------------------------------------------
- relates to
- 
                    JDK-8268483 Guidance on stream limitations in laziness and termination -           
- Open
 
-         
- 
                    JDK-8267452 Delegate forEachRemaining in Spliterators.iterator() -           
- Resolved
 
-         
- 
                    JDK-8267758 Double nested Stream.flatMap buffer the entire Stream before processing it -           
- Closed
 
-