繁体   English   中英

Java:比较嵌套哈希图的所有值

[英]Java : Compare all values of nested 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 );  

          }
     }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM