包含arraylist java中的元素hashmap

时间:2017-04-06 11:16:32

标签: java arraylist hashmap

我有... "moduleFileExtensions": [ "js", "jsx", "json" ], ... hashmap<integer,arraylist<integer>> H  我想测试arraylist中的第一个元素在hashmap中的第一个值是否相似 这就是我想要的

arraylist<integer> L

2 个答案:

答案 0 :(得分:0)

如果您只想检查第一个值,则无需迭代地图。

//assuming that your hash map H and array list L are not empty
List<Integer> firstArray = H.entrySet().iterator().next().getValue();
if (firstArray != null && firstArray.size() > 0 && firstArray.get(0).equals(L.get(0))) {
    //your code here
}

编辑:根据您的意见。如果要检查列表L中的每个值与映射H中列表中的每个项的相等性,那么您只需要在map循环中为列表添加循环。

Map<Integer, Integer> resultCounts = new HashMap<>(hmm.size());
int count;

for (Map.Entry<Integer, ArrayList<Integer>> e : hmm.entrySet()) {
    if (e.getValue() != null) {
        count = 0;
        //you can move this part to another method to avoid deep nested code
        for (Integer mapValue: e.getValue()) {
            for (Integer listValue: L) {
                if (mapValue.equals(listValue)) {
                    count++;
                }
            }
        }
        resultCounts.put(e.getKey(), count);
    }
}

答案 1 :(得分:0)

此代码可以帮助您

    Iterator i1 = listOperateur.iterator();
    for (Map.Entry<Integer, ArrayList<Integer>> e : hmm.entrySet()) {
       //if (e.getValue() != null) {
            count = 0;
            //you can move this part to another method to avoid deep nested code
            for (Integer mapValue : e.getValue()) {
                // for (Integer listValue : listOperateur) {
                if (mapValue.equals(listOperateur.get(k))) {
                    count++;
                    //  }
                }
            }
            sim.put(e.getKey(), count);
            k++;
      //  }
    }