Java : 比较嵌套hashmap的所有值
Java : Compare all values of nested hashmap
我有以下签名的 hashmap
Map<Key1, Map<Key2, List<Double>>>
我需要在列表中为内部嵌套映射的所有键添加相同的索引。我似乎找不到任何有效的方法来做到这一点,因为我事先不知道列表的大小。
到现在为止,我想出的最好办法是将所有内部列表转换为数组列表,然后使用迭代器将所有列表相加。有没有更有效的方法来做到这一点?
Double sumOfSameIndex =0.0;
Map<Key1, Map<Key2, List<Double>>> map = new LinkedHashMap<Key1, Map<Key2, List<Double>>>();
Iterator<Map.Entry<Key1, Map<Key2, List<Double>>>> outerIterator = map.entrySet().iterator();
while(outerIterator.hasNext()){
Map.Entry<Key1, Map<Key2, List<Double>>> entryForOuterMap= outerIterator.next();
Iterator<Map.Entry<Key2, List<Double>>> innerIterator = entryForOuterMap.getValue().entrySet().iterator();
while(innerIterator.hasNext()){
Map.Entry<Key2, List<Double>> entryForInnerMap = innerIterator.next();
// entryForInnerMap.getValue() returns List<Double>
List<Double> getList= entryForInnerMap.getValue();
// REPLACE THIS x WITH the INDEX OF ARRAY YOU WANT TO SUM
sumOfSameIndex = sumOfSameIndex + getList.get ( x );
}
}
我有以下签名的 hashmap
Map<Key1, Map<Key2, List<Double>>>
我需要在列表中为内部嵌套映射的所有键添加相同的索引。我似乎找不到任何有效的方法来做到这一点,因为我事先不知道列表的大小。 到现在为止,我想出的最好办法是将所有内部列表转换为数组列表,然后使用迭代器将所有列表相加。有没有更有效的方法来做到这一点?
Double sumOfSameIndex =0.0;
Map<Key1, Map<Key2, List<Double>>> map = new LinkedHashMap<Key1, Map<Key2, List<Double>>>();
Iterator<Map.Entry<Key1, Map<Key2, List<Double>>>> outerIterator = map.entrySet().iterator();
while(outerIterator.hasNext()){
Map.Entry<Key1, Map<Key2, List<Double>>> entryForOuterMap= outerIterator.next();
Iterator<Map.Entry<Key2, List<Double>>> innerIterator = entryForOuterMap.getValue().entrySet().iterator();
while(innerIterator.hasNext()){
Map.Entry<Key2, List<Double>> entryForInnerMap = innerIterator.next();
// entryForInnerMap.getValue() returns List<Double>
List<Double> getList= entryForInnerMap.getValue();
// REPLACE THIS x WITH the INDEX OF ARRAY YOU WANT TO SUM
sumOfSameIndex = sumOfSameIndex + getList.get ( x );
}
}