Apache common collection的使用(2)
来源:互联网 发布:网络和社会的关系论文 编辑:程序博客网 时间:2024/06/03 19:41
Closure 的使用
全部执行:把所有员工都执行salaryClosure操作
IteratorUtils.forEach(employees.iterator(), salaryClosure);
条件循环执行:循环执行salaryClosure,直到满足predicate则停止循环(类似复利的计算)
Closure<Employee> whileCol = WhileClosure.whileClosure(predicate, salaryClosure, false);
二选一执行:满足predicate的条件,则执行discount,否则执行subtract(如果满足打折的情况,则进行打折操作,如果不满足打折,则看是否可以减免)
Closure<Goods> ifClosure = IfClosure.ifClosure(predicate,discount, subtract);
顺序实行:先执行discount,再执行subtract(折上折:先打折再减免)
Closure<Goods> chainColsure = ChainedClosure.chainedClosure(discount, subtract);
package cn.others;import org.apache.commons.collections4.Closure;import org.apache.commons.collections4.IteratorUtils;import org.apache.commons.collections4.Predicate;import org.apache.commons.collections4.functors.WhileClosure;import java.util.ArrayList;import java.util.List;/** * @author Duoduo * @version 1.0 * @date 2017/4/15 13:27 */public class Test4 { public static void main(String[] args) { List<Employee> employees = new ArrayList<Employee>(); employees.add(new Employee("employee1", 100)); employees.add(new Employee("employee2", 200)); employees.add(new Employee("employee3", 300)); employees.add(new Employee("employee4", 400)); Closure<Employee> salaryClosure = new Closure<Employee>() { public void execute(Employee employee) { employee.setSalary(employee.getSalary() * 1.5); } }; //把所有的Employee都执行加薪操作 IteratorUtils.forEach(employees.iterator(), salaryClosure); System.out.println("=========== 执行所有人都加薪之后的结果 =============="); for (Employee employee : employees) { System.out.println(employee); } Predicate<Employee> predicate = new Predicate<Employee>() { public boolean evaluate(Employee employee) { return employee.getSalary() < 200; } }; //会一直按照百分百循环,直到满足谓词的判断条件 Closure<Employee> whileCol = WhileClosure.whileClosure(predicate, salaryClosure, false); IteratorUtils.forEach(employees.iterator(), whileCol); System.out.println("=========== 满足特定的条件一直循环执行的结果 =============="); for (Employee employee : employees) { System.out.println(employee); } }}
执行结果
=========== 执行所有人都加薪之后的结果 ==============
Employee{name=’employee1’, salary=150.0}
Employee{name=’employee2’, salary=300.0}
Employee{name=’employee3’, salary=450.0}
Employee{name=’employee4’, salary=600.0}
=========== 满足特定的条件一直循环执行的结果 ==============
Employee{name=’employee1’, salary=225.0}
Employee{name=’employee2’, salary=300.0}
Employee{name=’employee3’, salary=450.0}
Employee{name=’employee4’, salary=600.0}
package cn.others;import org.apache.commons.collections4.Closure;import org.apache.commons.collections4.IteratorUtils;import org.apache.commons.collections4.Predicate;import org.apache.commons.collections4.functors.ChainedClosure;import org.apache.commons.collections4.functors.IfClosure;import java.util.ArrayList;import java.util.List;/** * @author Duoduo * @version 1.0 * @date 2017/4/15 13:45 */public class Test5 { public static void main(String[] args){ List<Goods> goodss = new ArrayList<Goods>(); goodss.add(new Goods("goods1",100, true)); goodss.add(new Goods("goods2",200, false)); goodss.add(new Goods("goods3",300, true)); System.out.println("************ 原始数据 ****************"); for (Goods goods : goodss){ System.out.println(goods); } //减价20 Closure<Goods> subtract = new Closure<Goods>() { public void execute(Goods input) { input.setPrice(input.getPrice()-20); } }; //商品打折 Closure<Goods> discount = new Closure<Goods>() { public void execute(Goods input) { input.setPrice(input.getPrice()*0.9); } }; //判读是否打折 Predicate<Goods> predicate = new Predicate<Goods>() { public boolean evaluate(Goods goods) { return goods.isDiscount(); } }; //判断条件,如果满足谓词,则先执行打折,如果不满足,则执行扣减 Closure<Goods> ifClosure = IfClosure.ifClosure(predicate,discount, subtract); IteratorUtils.forEach(goodss.iterator(),ifClosure); System.out.println("************ 二选一执行结果 ****************"); for (Goods goods : goodss){ System.out.println(goods); } //先打折,再减免 Closure<Goods> chainColsure = ChainedClosure.chainedClosure(discount, subtract); IteratorUtils.forEach(goodss.iterator(), chainColsure); System.out.println("************ 折上折执行结果 ****************"); for (Goods goods : goodss){ System.out.println(goods); } }}
执行结果
** 原始数据 ******
Goods{name=’goods1’, price=100.0, discount=true}
Goods{name=’goods2’, price=200.0, discount=false}
Goods{name=’goods3’, price=300.0, discount=true}
** 二选一执行结果 ******
Goods{name=’goods1’, price=90.0, discount=true}
Goods{name=’goods2’, price=180.0, discount=false}
Goods{name=’goods3’, price=270.0, discount=true}
** 折上折执行结果 ******
Goods{name=’goods1’, price=61.0, discount=true}
Goods{name=’goods2’, price=142.0, discount=false}
Goods{name=’goods3’, price=223.0, discount=true}
- Apache common collection的使用(2)
- Apache common collection的使用(1)
- Apache common collection的使用(3)
- apache-collection的使用
- Apache Common Pool的使用
- apache common-dbutils 的使用
- Apache common FTP 的使用
- apache-common pool的使用
- apache-common pool的使用
- Apache Common Pool的使用
- apache-common pool的使用
- apache-common pool的使用
- apache-common pool 的使用
- 使用SolrCloud出现org.apache.solr.common.SolrException: Collection not found: my_solr2
- Apache Common HttpClient的使用之道
- 关于apache common lang ArrayUtils的使用
- Apache common-fileupload 组件的使用实例
- Java Collection Framework扩展Apache Common Collections介绍
- 如何使用腾讯云服务器搭建个人网站
- MySQL性能测试分析
- 移动端页面0.5px border的实现
- netstat和telnet命令在Windows7中的用法
- java基本语言元素
- Apache common collection的使用(2)
- 学习和思考
- 动态参数与静态参数的判断、修改
- mac如何升级g++的版本?更改默认gcc版本
- 一些web开发的插件好看的样式集合(不定时更新)
- 最长上升子序列
- java某年某月的天数
- 插入排序、希尔排序、堆排序
- strlen 函数的编写