一、Predicate断言
package Collections;
import java.util.ArrayList;
import java.util.List;
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;
/**
* 函数式编程之Predicate 断言
* 封装条件或判别式if else替代
* 1、 new EqualPredicate<类型>(值);
* EqualPredicate.equalPredicate(值);
*
* 2、 NotNullPredicate.notNullPredicate
* NotNullPredicate.INSTANCE
*
* PredicatedList.predicatedXxx(容器,判断)
*
* 3、 UniquePredicate.uniquePredicate()
*
* 4、 自定义 new Predicate类 + 重写eva luate方法
* PredicateUtils.allPredicate 多于两个
* andPredicate 两个
* anyPredicate 其中一个
*
*/
@SuppressWarnings("all")
public class Demo01 {
public static void main(String[] args) {
Test001();
Test002();
Test003();
Test004();
}
/**
* 比较相等判断
*/
public static void Test001()
{
System.out.println("=====相等判断=======");
Predicate
pre = new EqualPredicate
("liguodong"); //Predicate
pre = EqualPredicate.equalPredicate("liguodong");//同上 boolean flag = pre.eva luate("li"); System.out.println(flag); } /** * 非空判断 */ public static void Test002() { System.out.println("=====非空判断======="); Predicate notNull = NotNullPredicate.INSTANCE; //Predicate notNull = NotNullPredicate.notNullPredicate();//同上 String str = "lgd"; System.out.println(notNull.eva luate(str));//非空为true,否则为false。 //添加容器值得判断 List
list = PredicatedList.predicatedList(new ArrayList<>(), notNull); list.add(1000L); //list.add(null);//null值为false, 验证失败,出现异常 } public static void Test003() { System.out.println("=====唯一性判断======="); Predicate
uniquePre = UniquePredicate.uniquePredicate(); List
list = PredicatedList.predicatedList(new ArrayList
(),uniquePre); list.add(100L); list.add(200L); //list.add(100L);//出现重复值,抛出异常 } public static void Test004(){ System.out.println("=====自定义判断======="); //自定义的判别式 Predicate
selfPre = new Predicate
() { @Override public boolean eva luate(String object) { return object.length()>=5&&object.length()<=20; } }; Predicate notNull = NotNullPredicate.notNullPredicate();//非空 Predicate all = PredicateUtils.allPredicate(selfPre,notNull); List
list = PredicatedList.predicatedList(new ArrayList<>(), all); list.add("liguodong"); //list.add(null);//java.lang.NullPointerException //list.add("byby");//java.lang.IllegalArgumentException } }
运行结果:
=====相等判断=======
false
=====非空判断=======
true
=====唯一性判断=======
=====自定义判断=======
package Collections;
/**
* 员工类
*/
public class Employee {
private String name;
private double salary;
//alt+/
public Employee() {
}
//alt+shift+s +o
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
//alt+shift+s +r tab 回车 shift+tab 回车
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "(码农:"+this.name+",薪水:"+this.salary+")";
}
}
package Collections;
public class Level {
private String name;