服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - JAVA中对List进行查询

JAVA中对List进行查询

2021-11-15 10:55_东门吹雪 Java教程

下面小编就为大家带来一篇在java List中进行模糊查询的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

List查询

JAVA中从数据库中取数据,根据MyBits返回结果主要有两种类型的List,一种是List<Entity>,还一种是List<Map<String,Object>>,

 

那么怎样对List结果集进行查询操作呢?当然最常用的是用for循环遍历,但是这个效率比较低下,下面是我认为比较好的操作方式,

对List<Map<String,Object>>的查询

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * 从列表中找出包括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>的查询

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * 从列表中找出包括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;
}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/octdream/article/details/88963179

延伸 · 阅读

精彩推荐