Java stream 如何优雅地反向 filter

2018-02-11  本文已影响0人  风神AI

How to negate a method reference predicate

https://stackoverflow.com/questions/21488056/how-to-negate-a-method-reference-predicate

public static <T> Predicate<T> not(Predicate<T> t) {
    return t.negate();
}

Stream<String> s = ...;
long nonEmptyStrings = s.filter(not(String::isEmpty)).count();
上一篇 下一篇

猜你喜欢

热点阅读