CollectionUtils select方法 详解 用途~~~

来源:互联网 发布:人工智能是什么东西 编辑:程序博客网 时间:2024/06/05 22:56
我在作一个项目时规定不让用复杂的SQL如多表联接子查询等,在作一个报表时没有办法只有生成多个List结果集再用一大堆的if,for组合成一个List。其中有很都是从一个List中查找符合一定条件的对象,当时就用for+if 查找出后再放到一个新的List中,后来发现有更好的方法实现,方便多了。 

Java代码  收藏代码
  1. package mypackage;  
  2.   
  3. import java.math.BigDecimal;  
  4.   
  5. public class Student {  
  6. private String grade;  
  7. private int age;  
  8. private BigDecimal money;  
  9. public String getGrade() {  
  10.     return grade;  
  11. }  
  12.   
  13. public void setGrade(String grade) {  
  14.     this.grade = grade;  
  15. }  
  16.   
  17. public int getAge() {  
  18.     return age;  
  19. }  
  20.   
  21. public void setAge(int age) {  
  22.     this.age = age;  
  23. }  
  24.   
  25. public BigDecimal getMoney() {  
  26.     return money;  
  27. }  
  28.   
  29. public void setMoney(BigDecimal money) {  
  30.     this.money = money;  
  31. }  
  32. }  

Java代码  收藏代码
  1. package mypackage;  
  2.   
  3. import java.math.BigDecimal;  
  4.   
  5. import org.apache.commons.beanutils.PropertyUtils;  
  6. import org.apache.commons.collections.Predicate;  
  7.   
  8. public class MyPredicate implements Predicate {  
  9.     private String property;  
  10.   
  11.     private Object value;  
  12.   
  13.     public MyPredicate(String property, Object value) {  
  14.         this.property = property;  
  15.         this.value = value;  
  16.     }  
  17.   
  18.     public boolean evaluate(Object object) {  
  19.   
  20.         try {  
  21.             Object beanValue;  
  22.             if (property.indexOf(".") > 0) {  
  23.                 beanValue = PropertyUtils.getNestedProperty(object, property);  
  24.             } else {  
  25.                 beanValue = PropertyUtils.getProperty(object, property);  
  26.             }  
  27.             if (beanValue == null) {  
  28.                 return false;  
  29.             }  
  30.             if (!value.getClass().equals(beanValue.getClass())) {  
  31.                 throw new RuntimeException("value.class!=beanValue.class");  
  32.             }  
  33.             return myCompare(beanValue, value);  
  34.   
  35.         } catch (Exception e) {  
  36.             throw new RuntimeException(e.getMessage(), e.getCause());  
  37.         }  
  38.   
  39.     }  
  40.   
  41.     private boolean myCompare(Object value, Object beanValue) {  
  42.         if (beanValue.getClass().equals(Integer.class)) {  
  43.             if (((Integer) beanValue).equals(value)) {  
  44.                 return true;  
  45.             }  
  46.         }  
  47.         if (beanValue.getClass().equals(BigDecimal.class)) {  
  48.             if (((BigDecimal) beanValue).compareTo((BigDecimal) value) == 0) {  
  49.                 return true;  
  50.             }  
  51.         }  
  52.         if (beanValue.getClass().equals(String.class)) {  
  53.             if (beanValue.toString().equals(value.toString())) {  
  54.                 return true;  
  55.             }  
  56.         }  
  57.         return false;  
  58.     }  
  59.   
  60. }  

Java代码  收藏代码
  1. package mypackage;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.commons.collections.CollectionUtils;  
  7. import org.apache.commons.collections.Predicate;  
  8. import org.apache.commons.collections.PredicateUtils;  
  9.   
  10. public class TestPredicate {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) throws Exception {  
  16.   
  17.         List stants = new ArrayList();  
  18.         Student st1=new Student();  
  19.         Student st2=new Student();  
  20.         Student st3=new Student();  
  21.         Student st4=new Student();  
  22.         Student st5=new Student();  
  23.         Student st6=new Student();  
  24.         st1.setGrade("A");  
  25.         st1.setAge(5);  
  26.         st2.setGrade("A");  
  27.         st3.setGrade("A");  
  28.         st4.setGrade("B");  
  29.         st5.setGrade("C");  
  30.         st6.setGrade("D");  
  31.         stants.add(st1);  
  32.         stants.add(st2);  
  33.         stants.add(st3);  
  34.         stants.add(st4);  
  35.         stants.add(st5);  
  36.         stants.add(st6);      
  37.         Predicate isProblem =new MyPredicate("age",new Integer(5));  
  38.         //Predicate isProblem2 =new MyPredicate("grade","A");  
  39.         //Predicate any = PredicateUtils.anyPredicate(new Predicate[]{isProblem, isProblem2});  
  40.         List ddd=(List)CollectionUtils.select(stants,isProblem);  
  41.         System.out.println(ddd.size());   
  42.   
  43.     }  
  44.   
  45. }  

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"的对象。 
原创粉丝点击