注意 : 我使用jar包是commons-collections4-4.1.jar appache下的 各位不要导错
Predicate简单的使用
package com.hp.common;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.PredicateUtils;
import org.apache.commons.collections4.functors.EqualPredicate;
import org.apache.commons.collections4.functors.NotNullPredicate;
import org.apache.commons.collections4.functors.UniquePredicate;
import org.apache.commons.collections4.list.PredicatedList;
import org.apache.commons.collections4.map.PredicatedMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 函数式编程 -->Predicate
*/
public class Demo01 {
public static void main(String[] args) {
System.out.println("######相等判断");
boolean equalsePredicate = equalePredicate();
System.out.println(equalsePredicate);
System.out.println("####为空判断");
boolean str = notNull(null);
System.out.println(str);
System.out.println("####对集合进行的操作 判断集合是否为空");
predicateList();
System.out.println("####判断map是否为空");
predicateMap();
System.out.println("######唯一性判断");
unionPredicate();
System.out.println("#####z自定义验证");
customPridicate();
}
/**
* 比较相等的判断 相等返回true 不等返回false
*
* @return
*/
public static boolean equalePredicate() {
//相当于if else 的替代
//第一种需要 new EqualPredicate<String>("hello")
// Predicate<String> hello = new EqualPredicate<String>("hello");
//第二种 不需要new 对象 里面有封装好的方法
Predicate<String> hello = EqualPredicate.equalPredicate("hello");
//判断是否相等
return hello.evaluate("hello");
}
/**
* 对单个字符 进行判断是否为空 如果不为空返回true 为空返回false
*
* @param str
* @return
*/
public static boolea