Python 官方文档:入门教程 => 点击学习
目录List查询总结List查询 JAVA中从数据库中取数据,根据MyBits返回结果主要有两种类型的List,一种是List<Entity>,还一种是List<M
JAVA中从数据库中取数据,根据MyBits返回结果主要有两种类型的List,一种是List<Entity>,还一种是List<Map<String,Object>>,
那么怎样对List结果集进行查询操作呢?当然最常用的是用for循环遍历,但是这个效率比较低下,下面是我认为比较好的操作方式,
对List<Map<String,Object>>的查询
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;
}
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>的查询
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;
}
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注编程网的更多内容!
--结束END--
本文标题: JAVA中对List进行查询
本文链接: https://lsjlt.com/news/132340.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0