Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 590 Bytes

File metadata and controls

36 lines (25 loc) · 590 Bytes

Take While:

It keeps elements from the start of the stream as long as a condition is true. When the condition becomes false, the stream stops processing further elements.

List<Integer> numbers = List.of(1, 2, 3, 6, 2, 4);

List<Integer> result = numbers.stream()
        .takeWhile(n -> n < 5)
        .toList();

System.out.println(result);

Output:

[1, 2, 3]

Previous: H Skip

Next: J Drop While