A query in stream

var movies = List.of(
new Movie(“a”,10),
new Movie(“b”,20),
new Movie(“c”,30)
var mv = movies.stream().dropWhile(m->m.getLikes() == 20);
In the result I can see all 3 are getting printed.instead of dropping when it is 20.
why this code is not working?

The behavior of dropWhile behaves like a while loop. It keeps dropping elements until it finds one that fails the condition. In the list you are using, the first element fails the condition so it drops none of the elements and you get all three. You want a different stream method called filter and just flip your condition.

1 Like

In other words:

  .filter(m -> m.getLikes() != 20)
    m -> System.out.println(m.getLikes()));
1 Like