Apache common collection的使用(1)
来源:互联网 发布:淘宝夜间模式 编辑:程序博客网 时间:2024/06/04 00:59
Predicate 的使用 (主要用于数据的逻辑判断)
唯一性判断:当插入相同的数据时,则报错
Predicate uniquePredicate = UniquePredicate.uniquePredicate();
非空判断: 当插入的数据为空时,则报错
Predicate notNullPredicate = NotNullPredicate.notNullPredicate();
相等判断:判断值是否相同
Predicate predicate = EqualPredicate.equalPredicate("Test");
自定义判断:(重写函数)实现自定义的逻辑
//自定义谓词Predicate<String> selfPredicate = new Predicate<String>() { public boolean evaluate(String s) { return (s.length() > 5); }};
package cn.others;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;import java.util.ArrayList;import java.util.List;/** * @author Duoduo * @version 1.0 * @date 2017/4/15 12:31 */public class Test1 { public static void main(String[] args) { //自定义谓词 Predicate<String> selfPredicate = new Predicate<String>() { public boolean evaluate(String s) { return (s.length() > 5); } }; Predicate notNullPredicate = NotNullPredicate.notNullPredicate(); Predicate allPredicate = PredicateUtils.allPredicate(selfPredicate, notNullPredicate); List<String> list = PredicatedList.predicatedList(new ArrayList<String>(), allPredicate); list.add(null); list.add("1212"); } public static void uniquePredicate() { System.out.println("************** 唯一判断 ***************"); Predicate uniquePredicate = UniquePredicate.uniquePredicate(); List<String> list = PredicatedList.predicatedList(new ArrayList<String>(), uniquePredicate); list.add("1"); list.add("1"); } public static void equalPredicate() { System.out.println("************** 相等判断 ***************"); Predicate predicate = EqualPredicate.equalPredicate("Test"); System.out.println(predicate.evaluate("Test1111111")); } public static void notNullPredicate() { System.out.println("************** 非空判断 ***************"); Predicate notNullPredicate = NotNullPredicate.notNullPredicate(); List<String> list = PredicatedList.predicatedList(new ArrayList<String>(), notNullPredicate); list.add("11111"); list.add(null); }}
Transformer 类型转化函数(例如:日期格式化)
package cn.others;import org.apache.commons.collections4.CollectionUtils;import org.apache.commons.collections4.Transformer;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collection;import java.util.List;/** * @author Duoduo * @version 1.0 * @date 2017/4/15 13:00 */public class Test2 { public static void main(String[] args) { System.out.println("内置类型转化, 日期格式化输出"); Transformer<Long, String> transformer = new Transformer<Long, String>() { public String transform(Long aLong) { return new SimpleDateFormat("yyyy-MM-dd").format(aLong); } }; List<Long> list = new ArrayList<Long>(); list.add(1000000000L); list.add(2000000000L); Collection<String> collections = CollectionUtils.collect(list, transformer); System.out.println(list); for (String string:collections){ System.out.println(string); } }}
运行结果如下:
内置类型转化, 日期格式化输出[1000000000, 2000000000]1970-01-121970-01-24
Predicate 和 Transformer 的结合使用(主要用于业务的转化)
需求:当收入 < 1000 时,输出 卖身中。。。
当收入 >= 1000 时,输出 养身中。。。
实现思路:
1. 利用Predicate来进行数据判断大小
2. 利用Transformer来完成类型的转化
优势:完成业务判断和数据的处理的解耦
定义Employee类
package cn.others;/** * @author Duoduo * @version 1.0 * @date 2017/4/15 13:08 */public class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } 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 "Employee{" + "name='" + name + '\'' + ", salary=" + salary + '}'; }}
定义Level类
package cn.others;/** * @author Duoduo * @version 1.0 * @date 2017/4/15 13:09 */public class Level { private String name; private String level; public Level(String name, String level) { this.name = name; this.level = level; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } @Override public String toString() { return "Level{" + "name='" + name + '\'' + ", level='" + level + '\'' + '}'; }}
程序处理部分
package cn.others;import org.apache.commons.collections4.CollectionUtils;import org.apache.commons.collections4.Predicate;import org.apache.commons.collections4.Transformer;import org.apache.commons.collections4.functors.SwitchTransformer;import java.util.ArrayList;import java.util.Collection;import java.util.List;/** * 解耦,处理数据和判断数据分离 * 实现思路: * 1.定义两个Predicate,一个为 <1000 一个为 >=1000 * 2.定义两个Transformer,一个为 <1000 时需要转义的处理, 一个为 >=1000 时需要转义的处理 * 3.利用 SwitchTransformer 把 Predicate 和 Transformer 关联起来(特别需要注意顺序) * @author Duoduo * @version 1.0 * @date 2017/4/15 13:12 */public class Test3 { public static void main(String[] args){ //定义判断谓词 Predicate<Employee> lowSalaryPre = new Predicate<Employee>() { public boolean evaluate(Employee employee) { return employee.getSalary()<1000; } }; Predicate<Employee> highSalaryPre = new Predicate<Employee>() { public boolean evaluate(Employee employee) { return employee.getSalary()>=1000; } }; //定义转换器 Transformer<Employee, Level> lowTrans = new Transformer<Employee, Level>() { public Level transform(Employee employee) { return new Level(employee.getName(), "卖身中。。。。。"); } }; Transformer<Employee, Level> highTrans = new Transformer<Employee, Level>() { public Level transform(Employee employee) { return new Level(employee.getName(), "养身中。。。。。"); } }; Predicate[] allPre = {lowSalaryPre, highSalaryPre}; Transformer[] allTrans = {lowTrans, highTrans}; //把转化器和判断谓词进行关联 Transformer switchTrans = new SwitchTransformer(allPre, allTrans, null); List<Employee> list = new ArrayList<Employee>(); list.add(new Employee("test1",100)); list.add(new Employee("test2",10000)); //记性转化 Collection<Level> collections = CollectionUtils.collect(list, switchTrans); for (Level level : collections){ System.out.println(level); } }}
运行结果
Level{name=’test1’, level=’卖身中。。。。。’}
Level{name=’test2’, level=’养身中。。。。。’}
0 0
- Apache common collection的使用(1)
- Apache common collection的使用(2)
- 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介绍
- Linux下的QQ
- Factors in Finetuning Deep Model for Object Detection with Long-tail Distribution解读
- 第八周:[Leetcode]5. Longest Palindromic Substring
- Java数据集合-学习总结
- 九个隐含JSP对象
- Apache common collection的使用(1)
- orcale库表增加一个字段,自动设置为系统默认时间
- 骨骼openni_tracker has problem opening database/parameter File
- nginx学习7——日志建立以及分割
- jQuery选择器之可见性筛选选择器
- VC/MFC 获取记事本的内容
- 论文阅读 第一篇:mutual context model
- null,undefined 的区别?
- linux下的进程描述符——task_struct