设为首页 加入收藏

TOP

Apache Commons Collections基本操作(Predicate、Transformat、Closure等)(三)
2015-11-21 01:01:37 来源: 作者: 【 】 浏览:18
Tags:Apache Commons Collections 基本操作 Predicate Transformat Closure
; } 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 Goods {
    private String name;
    private double price;
    private boolean discount;//折扣
    public Goods() {
    }
    public Goods(String name, double price, boolean discount) {
        super();
        this.name = name;
        this.price = price;
        this.discount = discount;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public boolean isDiscount() {
        return discount;
    }
    public void setDiscount(boolean discount) {
        this.discount = discount;
    }

    @Override
    public String toString() {
        return "(商品:"+this.name+",价格:"+this.price+",是否打折:"+(discount?"是":"否")+")";      
    }
}
package Collections;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.ChainedClosure;
import org.apache.commons.collections4.functors.IfClosure;
import org.apache.commons.collections4.functors.WhileClosure;

/**
 * 函数式编程Closure 闭包封装业务功能
 *  1.  Closure
 *      CollectionUtils.forAllDo(容器,功能类对象)
 *  
 *  2.  IfClosure    
 *      IfClosure.ifClosure(断言,功能1,功能2)
 *      CollectionUtils.forAllDo(容器,功能类对象)
 *  
 *  3.  WhileClosure
 *      WhileClosure.whileClosure(断言,功能,标识符)
 *      CollectionUtils.forAllDo(容器,功能类对象)
 *  
 *  4.  ChainedClosure
 *      ChainedClosure.chainedClosure(功能列表)
 *      CollectionUtils.forAllDo(容器,功能类对象)
 * @author liguodong
 */

@SuppressWarnings("all")
public class Demo03 {
    public static void main(String[] args) {
        basic();
        System.out.println("==================");
        ifClousure();
        System.out.println("==================");
        whileClosure();
        System.out.println("==================");
        chainClousure();
    }


    //基本操作
    public static void basic()
    {
        //数据
        List
   
     empList = new ArrayList<>(); empList.add(new Employee("mark",20000)); empList.add(new Employee("json",10000)); empList.add(new Employee("Ivy",5000)); //业务功能 Closure
    
      cols = new Closure
     
      () { @Override public void execute(Employee emp) { emp.setSalary(emp.getSalary()*1.2); } }; //工具类 CollectionUtils.forAllDo(empList, cols); //操作后的数据 Iterator
      
        empIt = empList.iterator(); while(empIt.hasNext()) { System.out.println(empIt.next()); } } /** * 二选一 如果打折商品,进行9折;否则满百减20。 */ public static void ifClousure() { List
       
         goodsList = new ArrayList<>(); goodsList.add(new Goods("android视频",120,true)); goodsList.add(new Goods("javaee视频",80,false)); goodsList.add(new Goods("hadoop视频",150,false)); //满百减20 Closure
        
          subtract = new Closure
         
          () { @Override public void execute(Goods input) { if(input.getPrice()>=100){ input.setPrice(input.getPrice()-20); } } }; //打折 Closure
          
            discount = new Closure
           
            () { @Override public void execute(Goods input) { if(input.isDiscount()){ input.setPrice(input.getPrice()*0.9); } } }; //判断 Predicate
            
              pre = new Predicate
             
              () { @Overrid
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 3/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++ string 类 学习笔记 下一篇C++――2828: 素数判断

评论

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