CollectionUtils select方法 详解 用途~~~
来源:互联网 发布:人工智能是什么东西 编辑:程序博客网 时间:2024/06/05 22:56
我在作一个项目时规定不让用复杂的SQL如多表联接子查询等,在作一个报表时没有办法只有生成多个List结果集再用一大堆的if,for组合成一个List。其中有很都是从一个List中查找符合一定条件的对象,当时就用for+if 查找出后再放到一个新的List中,后来发现有更好的方法实现,方便多了。
MyPredicate实现了Predicate接口来定义你的规则
Predicate isProblem =new MyPredicate("age",new Integer(5));
这里指定要找age为5的Student
List ddd=(List)CollectionUtils.select(stants,isProblem);
在调用CollectionUtils.select就可以查到你想要的。
你还可以用PredicateUtils中的方法生成更复杂的条件
这里我定义了第2个Predicate isProblem2来指定grade为A
PredicateUtils.anyPredicate(new Predicate[]{isProblem, isProblem2});
就是符合isProblem或isProblem2。这样查出的就是age==5 or grade=="A"的对象。
- package mypackage;
- import java.math.BigDecimal;
- public class Student {
- private String grade;
- private int age;
- private BigDecimal money;
- public String getGrade() {
- return grade;
- }
- public void setGrade(String grade) {
- this.grade = grade;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public BigDecimal getMoney() {
- return money;
- }
- public void setMoney(BigDecimal money) {
- this.money = money;
- }
- }
- package mypackage;
- import java.math.BigDecimal;
- import org.apache.commons.beanutils.PropertyUtils;
- import org.apache.commons.collections.Predicate;
- public class MyPredicate implements Predicate {
- private String property;
- private Object value;
- public MyPredicate(String property, Object value) {
- this.property = property;
- this.value = value;
- }
- public boolean evaluate(Object object) {
- try {
- Object beanValue;
- if (property.indexOf(".") > 0) {
- beanValue = PropertyUtils.getNestedProperty(object, property);
- } else {
- beanValue = PropertyUtils.getProperty(object, property);
- }
- if (beanValue == null) {
- return false;
- }
- if (!value.getClass().equals(beanValue.getClass())) {
- throw new RuntimeException("value.class!=beanValue.class");
- }
- return myCompare(beanValue, value);
- } catch (Exception e) {
- throw new RuntimeException(e.getMessage(), e.getCause());
- }
- }
- private boolean myCompare(Object value, Object beanValue) {
- if (beanValue.getClass().equals(Integer.class)) {
- if (((Integer) beanValue).equals(value)) {
- return true;
- }
- }
- if (beanValue.getClass().equals(BigDecimal.class)) {
- if (((BigDecimal) beanValue).compareTo((BigDecimal) value) == 0) {
- return true;
- }
- }
- if (beanValue.getClass().equals(String.class)) {
- if (beanValue.toString().equals(value.toString())) {
- return true;
- }
- }
- return false;
- }
- }
- package mypackage;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.commons.collections.CollectionUtils;
- import org.apache.commons.collections.Predicate;
- import org.apache.commons.collections.PredicateUtils;
- public class TestPredicate {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- List stants = new ArrayList();
- Student st1=new Student();
- Student st2=new Student();
- Student st3=new Student();
- Student st4=new Student();
- Student st5=new Student();
- Student st6=new Student();
- st1.setGrade("A");
- st1.setAge(5);
- st2.setGrade("A");
- st3.setGrade("A");
- st4.setGrade("B");
- st5.setGrade("C");
- st6.setGrade("D");
- stants.add(st1);
- stants.add(st2);
- stants.add(st3);
- stants.add(st4);
- stants.add(st5);
- stants.add(st6);
- Predicate isProblem =new MyPredicate("age",new Integer(5));
- //Predicate isProblem2 =new MyPredicate("grade","A");
- //Predicate any = PredicateUtils.anyPredicate(new Predicate[]{isProblem, isProblem2});
- List ddd=(List)CollectionUtils.select(stants,isProblem);
- System.out.println(ddd.size());
- }
- }
MyPredicate实现了Predicate接口来定义你的规则
Predicate isProblem =new MyPredicate("age",new Integer(5));
这里指定要找age为5的Student
List ddd=(List)CollectionUtils.select(stants,isProblem);
在调用CollectionUtils.select就可以查到你想要的。
你还可以用PredicateUtils中的方法生成更复杂的条件
这里我定义了第2个Predicate isProblem2来指定grade为A
PredicateUtils.anyPredicate(new Predicate[]{isProblem, isProblem2});
就是符合isProblem或isProblem2。这样查出的就是age==5 or grade=="A"的对象。
- CollectionUtils select方法 详解 用途~~~
- CollectionUtils.select用法
- CollectionUtils的常用方法
- sql中select语句详解及用途
- CollectionUtils
- apache common包 CollectionUtils 使用 详解
- Jsoup中select方法详解
- Jsoup中select方法详解
- select 1 from的用途
- select 1 from的用途
- oracle数据库---select的用途
- CollectionUtils各种方法,总有一个满足你
- ClassLoader 详解及用途
- ClassLoader 详解及用途
- ClassLoader 详解及用途
- ClassLoader详解及用途
- ClassLoader 详解及用途
- ClassLoader 详解及用途
- 使用前端框架Foundation 4来帮助简化响应式设计开发
- Java2 核心技术第七版、第八版、第九版源代码下载地址
- Flash builder 4内存优化之java heap space解决办法
- maven常见问题问答
- 清除SVN隐藏目录
- CollectionUtils select方法 详解 用途~~~
- HTML嵌套规则
- oracle改变字符编码(ORA-12899问题)
- vi/vim 常用命令
- JEECG
- 日期范围选择公用函数,传参为选择的textField
- C# 调用Windows API(1)
- 多线程实例之简单的卖票程序 ------记录与毕向东老师视频
- redis 集群设计一种方案