In scala, these methods work fine, but in java9 dropWhile works differently, I think.
Here is an example for takeWhile
Stream.of("a", "b", "c", "de", "f", "g", "h") .peek(System.out::println) .takeWhile(s -> s.length() <= 1) .collect(Collectors.toList());
The output is beautiful: a, b, c, de, [a, b, c] It does not process elements after "de", so it works as expected
But dropWhile works differently what I expect:
Stream.of("a", "b", "c", "de", "f", "g", "h") .peek(s -> System.out.print(s + ", ")) .dropWhile(s -> s.length() <= 1) .collect(Collectors.toList());
Output: a, b, c, de, f, g, h, [de, f, g, h]
Thus, it does not stop after the de element, it processes the entire collection.
Why does he handle the entire collection? I know that he needed to take all the elements and collect them in the List, but should he not stop processing after the "de" element?