如何只在List <map <string,object =“”>&gt;中迭代一个值

时间:2018-05-26 11:09:41

标签: java collections

以下是我的意见,

List<Map<String, Object>> other =   [{money=2, data=abc}, {money=3, data=xyz}]

我想在每个地图中仅迭代money并忽略每个地图中的数据。如何只迭代钱

  Map<String, Object> finalMap;
                for(Map<String, Object> child : other){
                    for(Object key : child.values()){
                        if(other.contains(((BigDecimal)key).intValue())){
                            finalMap.putAll(child);
                        }
                    }
                }

2 个答案:

答案 0 :(得分:0)

for (String key : map.keySet()) {
    // your code
}

这应该这样做。在你的情况下,它将是:

List<Map<String, Object>> finalMap;for(Map<String, Object> child : other) {
    for(Object key : child.keySet()()) {
        // ...
    }
}

答案 1 :(得分:0)

使用Java 8的流API,它看起来像这样:

0
len = 2, cap = 2
1
len = 3, cap = 4
2
len = 4, cap = 4
3
len = 5, cap = 8
4
len = 6, cap = 8
5
len = 7, cap = 8
6
len = 8, cap = 8
7
len = 9, cap = 16
8
len = 10, cap = 16
9
len = 11, cap = 16
10
len = 12, cap = 16
11
len = 13, cap = 16
12
len = 14, cap = 16
13
len = 15, cap = 16
14
len = 16, cap = 16

这将迭代列表并从hashmap获取密钥“money”。