List<String> list1 = new ArrayList<>(Arrays.asList("1", "2", "3", "1", "1"));
List<String> list2 = new ArrayList<>(Arrays.asList("1", "3"));
System.out.println(CollectionUtils.countMatches(list1, new Predicate() {
@Override
public boolean evaluate(Object object) {
return ((String)object).equals("1");
}
}));
System.out.println((CollectionUtils.countMatches(list1,m->((String)m).equals("1"))));
// 若元素等于 "1" 则返回true, PredicateUtils.equalPredicate("1").evaluate()
// return (iValue.equals(object));
System.out.println(CollectionUtils.countMatches(list1, PredicateUtils.equalPredicate("1")));
// 统计true的个数: return true
System.out.println(CollectionUtils.countMatches(list1, PredicateUtils.truePredicate()));
// 统计false的个数: return false
System.out.println(CollectionUtils.countMatches(list1, PredicateUtils.falsePredicate()));
// 统计null的个数: return (object == null)
System.out.println(CollectionUtils.countMatches(list1, PredicateUtils.nullPredicate()));
// 统计不为null的个数: return (object != null)
System.out.println(CollectionUtils.countMatches(list1, PredicateUtils.notNullPredicate()));
// return (iValue == object);
System.out.println(CollectionUtils.countMatches(list1, PredicateUtils.identityPredicate("1")));
// return (iType.isInstance(object));
System.out.println(CollectionUtils.countMatches(list1, PredicateUtils.instanceofPredicate(String.class)));
// 第一次插入元素返回true,存到set中
// return iSet.add(object);
Predicate predicate = PredicateUtils.uniquePredicate();
System.out.println(predicate.evaluate("1"));
System.out.println(predicate.evaluate("1"));
System.out.println(predicate.evaluate("2"));
// (iPredicate1.evaluate(object) && iPredicate2.evaluate(object));
Predicate predicate1 = PredicateUtils.andPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate());
System.out.println("predicate1 : " + predicate1.evaluate(1)); // predicate1 : true
// 其中一个Predicate 返回fasle则返回false
Predicate predicate2 = PredicateUtils.allPredicate(new Predicate[]{PredicateUtils.truePredicate()});
System.out.println("predicate2 : " + predicate2.evaluate(1)); // predicate2 : true
// 其中一个Predicate 返回true 则返回true : (iPredicate1.evaluate(object) || iPredicate2.evaluate(object));
Predicate predicate3 = PredicateUtils.orPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate());
System.out.println("predicate3 : " + predicate3.evaluate(1)); // predicate3 : true
// 任何一个Predicate 返回true则返回true
Predicate predicate4 = PredicateUtils.anyPredicate(new Predicate[]{PredicateUtils.truePredicate()});
System.out.println("predicate4 : " + predicate4.evaluate(1)); // predicate4 : true
// 只有一个Predicate 为true 才返回true
Predicate predicate5 = PredicateUtils.onePredicate(new Predicate[]{PredicateUtils.truePredicate()});
System.out.println("predicate5 : " + predicate5.evaluate(1)); // predicate5 : true
// Evaluates the predicate returning false if any stored predicate returns false.
// 集合为 nonePredicate
Predicate predicate6 = PredicateUtils.neitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate());
System.out.println("predicate6 : " + predicate6.evaluate(1)); // predicate6 : false
// NotPredicate : return !(iPredicate.evaluate(object));
System.out.println(CollectionUtils.countMatches(list1, PredicateUtils.notPredicate(PredicateUtils.equalPredicate("1")))); // 2
// nullIsExceptionPredicate : 传入参数(evaluate(Object object) ,object is null)为空则报错
// nullIsFalsePredicate : 传入参数(evaluate(Object object) ,object is null)为空则报返回false,否则 return iPredicate.evaluate(object);
// nullIsTruePredicate : 同理