浏览 7288 次
锁定老帖子 主题:从collection中查找对象
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2006-09-22
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"的对象。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2006-09-22
你写的通用性严重不足嘛,有个现成的JoSQL。 http://josql.sourceforge.net/index.html a very cool API that i bet most of us will be interested in. here is part of the Quick Start available from its home page: List myObjs = getMyObjects (); // Get a list of java.io.File objects. Query q = new Query (); // Create a new Query. q.parse ("SELECT * FROM java.io.File WHERE name LIKE '%.java'"); QueryResults qr = q.execute (myObjs); // Execute the query. List res = qr.getResults (); // Cycle over the query results. as you can find the JoSQL treat the File objects like the database table and its attributes like the database fields. |
|
返回顶楼 | |
发表时间:2006-09-22
引用 你写的通用性严重不足嘛
我知道啊,只是试一下,看看可不可行 |
|
返回顶楼 | |