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
原创粉丝点击