设为首页 加入收藏

TOP

Apache Commons Collections基本操作(Predicate、Transformat、Closure等)(一)
2015-11-21 01:01:37 来源: 作者: 【 】 浏览:20
Tags:Apache Commons Collections 基本操作 Predicate Transformat Closure

一、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
=====唯一性判断=======
=====自定义判断=======

二、Transformat 类型转换

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;
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++ string 类 学习笔记 下一篇C++――2828: 素数判断

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: