PredicateUtils工具类

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 : 同理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值