设为首页 加入收藏

TOP

Apache Commons Collections基本操作(Predicate、Transformat、Closure等)(五)
2015-11-21 01:01:37 来源: 作者: 【 】 浏览:13
Tags:Apache Commons Collections 基本操作 Predicate Transformat Closure
e public boolean eva luate(Goods goods) { return goods.isDiscount(); } }; //二选一 Closure ifClo = IfClosure.ifClosure(pre,discount,subtract); //关联 CollectionUtils.forAllDo(goodsList,ifClo); //查看操作后的数据 for(Goods temp:goodsList) { System.out.println(temp); } } /** * 确保所有的员工工资都大于10000,如果已经超过的不再上涨 。 */ public static void whileClosure() { //数据 List empList = new ArrayList<>(); empList.add(new Employee("周杰伦",20000)); empList.add(new Employee("范冰冰",30000)); empList.add(new Employee("黄晓明",5000)); //业务功能 每次上涨0.2 Closure cols = new Closure () { @Override public void execute(Employee emp) { emp.setSalary(emp.getSalary()*1.2); } }; //判断 Predicate empPre = new Predicate () { @Override public boolean eva luate(Employee emp) { return emp.getSalary()<10000; } }; //false 表示while结构先判断后执行 //true 表示do..while先执行后判断 Closure whileCols = WhileClosure.whileClosure(empPre,cols,false); //工具类 CollectionUtils.forAllDo(empList, whileCols); //操作后的数据 Iterator empIt = empList.iterator(); while(empIt.hasNext()) { System.out.println(empIt.next()); } } /** *折上减 如果打折商品,先进行9折,如果还满百,再减20 */ public static void chainClousure() { List goodsList = new ArrayList<>(); goodsList.add(new Goods(" Android视频",120,true)); goodsList.add(new Goods("javaee视频",100,false)); goodsList.add(new Goods("Spack视频",80,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); } } }; //链式操作 Closure chinaClo = ChainedClosure.chainedClosure(discount,subtract); //关联 CollectionUtils.forAllDo(goodsList,chinaClo); //查看操作后的数据 for(Goods temp:goodsList) { System.out.println(temp); } } }

运行结果:

(码农:mark,薪水:24000.0)
(码农:json,薪水:12000.0)
(码农:Ivy,薪水:6000.0)
==================
(商品:android视频,价格:108.0,是否打折:是)
(商品:javaee视频,价格:80.0,是否打折:否)
(商品:hadoop视频,价格:130.0,是否打折:否)
==================
(码农:周杰伦,薪水:20000.0)
(码农:范冰冰,薪水:30000.0)
(码农:黄晓明,薪水:10368.0)
==================
(商品:Android视频,价格:88.0,是否打折:是)
(商品:javaee视频,价格:80.0,是否打折:否)
(商品:Spack视频,价格:80.0,是否打折:否)

四、集合操作

package Collections;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;

/**
 * 集合操作
 * 1、并集 CollectionUtils.union
 * 2、交集 CollectionUtils.intersection
 *       CollectionUtils.retainAll
 * 3、差集
 *       CollectionUtils.subtract
 */
public class Demo04 {
    public static void main(String[] args) {
        Set
   
     set1 = new HashSet<>(); set1.add(1); set1.add(2); set1.add(3); Set
    
      set2 = new HashSet<>(); set2.add(2); set2.add(3); set2.add(4); System.out.println("========并集=========="); //并集 Collection
     
       col = CollectionUtils.union(set1, set2); for(Integer temp:col) { System.out.print(temp+" "); } System.out.println("\n=========交集========="); //交集 //col = CollectionUtils.intersection(set1, set2); col = CollectionUtils.retainAll(set1, set2); for(Integer temp:col) { System.out.print(temp+" "); } //差集 System.out.println("\n=========差集========="); col = CollectionUtils.subtract(set1, set2); for(Integer temp:col) { System.out.print(temp+" "); } } } 
     
    
   

运行结果:

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

评论

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