论坛首页 Java企业应用论坛

从collection中查找对象

浏览 7288 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-09-22  
我在作一个项目时规定不让用复杂的SQL如多表联接子查询等,在作一个报表时没有办法只有生成多个List结果集再用一大堆的if,for组合成一个List。其中有很都是从一个List中查找符合一定条件的对象,当时就用for+if 查找出后再放到一个新的List中,后来发现有更好的方法实现,方便多了。

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"的对象。
   发表时间: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.





0 请登录后投票
   发表时间:2006-09-22  
引用
你写的通用性严重不足嘛

我知道啊,只是试一下,看看可不可行
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics