Java 8 stream foreach filter anyMatch

forEach the cycle

someObjects.forEach(obj -> { //to do something })

If this cycle's goal is to find the first element matches a predicate

Optional<SomeObject> result = someObjects.stream().filter(obj -> some_condition_met).findFirst();

 

If you just want to know if there is an element of the condition is true in the collection, you can use anyMatch:

boolean result = someObjects.stream().anyMatch(obj -> some_condition_met);

distinct()  :字符串去重

Guess you like

Origin www.cnblogs.com/MisMe/p/11470511.html