list查询
java中从数据库中取数据,根据mybits返回结果主要有两种类型的list,一种是list<entity>,还一种是list<map<string,object>>,
那么怎样对list结果集进行查询操作呢?当然最常用的是用for循环遍历,但是这个效率比较低下,下面是我认为比较好的操作方式,
对list<map<string,object>>的查询
/**
* 从列表中找出包括value值的列表
* @param list
* @param keyname
* @param value
* @return
*/
public static list<map<string,object>> finddatafromlistmap(list<map<string,object>> list,string keyname,object value) {
predicate<map<string,object>>predicate = new predicate<map<string,object>>() {
@override
public boolean evaluate(map<string,object> map) {
// todo auto-generated method stub
try {
object val = map.get(keyname);
if(value == null || val == null)
return false;
return val.equals(value);
}
catch (exception e) {
return false;
}
}
};
list<map<string,object>> result = (list<map<string,object>>) collectionutils.select( list, predicate);
return result;
}
/**
* 多值将交叉查找
* @param list
* @param keyname2valuemap
* @return
*/
public static list<map<string,object>> finddatafromlistmapbykeyvalues(
list<map<string,object>> list,
map<string,object> keyname2valuemap) {
predicate<map<string,object>>predicate = new predicate<map<string,object>>() {
@override
public boolean evaluate(map<string,object> map) {
// todo auto-generated method stub
try {
for(map.entry<string,object> entry : keyname2valuemap.entryset()){
string keyname = entry.getkey();
object value = entry.getvalue();
object val = map.get(keyname);
if(stringutils.isblank(keyname) || value == null || val == null)
return false;
if(!val.equals(value))
return false;
}
return true;
}
catch (exception e) {
return false;
}
}
};
list<map<string,object>> result = (list<map<string,object>>) collectionutils.select( list, predicate);
return result;
}
对list<entity>的查询
/**
* 从列表中找出包括value值的对象列表
* @param list
* @param methodname
* @param value
* @return
*/
public static <t,v> list<t> findobjfromlist(list<t> list,string methodname,v value) {
predicate<t>predicate = new predicate<t>() {
@override
public boolean evaluate(t obj) {
// todo auto-generated method stub
try {
method method = obj.getclass().getmethod(methodname);
@suppresswarnings("unchecked")
v keyname = (v) method.invoke(obj);
return keyname.equals(value);
}
catch (exception e) {
return false;
}
}
};
list<t> result = (list<t>) collectionutils.select( list, predicate);
return result;
}
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!