What is the difference between Java – takeWhile and filters?
•
Java
How is takeWhile () different from filter () in Java 9 What additional utilities does it have?
Stream.of(1,2,3,4,5,6,7,8,9,10).filter(i -> i < 4 ) .forEach(System.out::println);
This may be what the following will do
Stream.of(1,10).takeWhile(i -> i < 4 ) .forEach(System.out::println);
So what does this new feature need?
Solution
Filter will delete all items that do not meet the conditions from the stream
TakeWhile will abort the flow the first time an item does not meet the criteria
for example
Stream.of(1,10,1) .filter(i -> i < 4 ) .forEach(System.out::print);
Will print
but
Stream.of(1,1) .takeWhile(i -> i < 4 ) .forEach(System.out::print);
Will print
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码