简体   繁体   English

对List <>中包含的HashMap <>进行迭代,以获取Map中包含的数组变量作为Java中的值的附加值

[英]Iteration of HashMap<> included in List<> to get addition of array variables included in map as values in java

I have BankDetails as pojo object with following variables and its getters and setters. 我将BankDetails作为pojo对象,并具有以下变量及其getter和setters。

private String bankName;
private  Map<String,Long[]>  monthMap= newHashMap<String,Long[]>();
private String month;

now I have created List with each bean of BankDetails stored in list with multiple keys and values in its map eg 现在我已经创建了List,其中BankDetails的每个bean都存储在list中,并且在其map中具有多个键和值,例如

for one bean I have 我有一个豆

map1 = 05, [8,0,0] ;map2 = 06, [6,0,4] ;map3 = 07, [45,3,0] ;map4 = 08, [0,56,9] ;

for second bean I have 第二个豆我有

map1 = 05, [0,0,0] ;map2 = 06, [8,0,4] ;map3 = 07, [5,0,1] ;map4 = 08, [2,6,8] ;

... ...

Where 05,06,07 and 08 are months. 其中05,06,07和08是个月。

now I want to have sum of all map1s map2s map3s and map4s from all beans vertically. 现在我想垂直获取所有bean中所有map1s map2s map3s和map4s的总和。

now, what I want is to have grandTotal monthly of array elements, of all List objects and return List which will have only 4 objects having total of all four as shown below. 现在,我想要的是每月拥有所有列表对象的array元素的grandTotal,并返回List,其中List将只有4个对象,总共四个,如下所示。

map1- 05, [8, 0 ,0] ; map2- 06, [12,0,8] ; map3- 07,[50,3,1] : map4- 08, [2,62,17]

please suggest how can I do it. 请提出我该怎么做。 Being newBee in java really weak in iterations and logic. 在Java中成为newBee确实在迭代和逻辑方面很弱。

You can do something like this: 您可以执行以下操作:

    // Declare a ArrayList of Map Objects
    ArrayList<Map<String,Long[]>> mapsList = new ArrayList<>();

    // add all the beans
    mapsList.add(monthMap);
    mapsList.add(monthMap2);
    .
    .


    // declare a map for storing the sum
    Map<String,Long[]> sumMap = new HashMap<>();

    // for every key in map (assuming maps have the same amount 
    // of key-value pairs)
    for(String str : mapsList.get(0).keySet()){
        // initialising sum for long arrays
        Long[] sum = {0L,0L,0L};
        // for every map bean
        for (int i = 0; i < mapsList.size(); i++){
            // for every column in long array of map
            for (int j = 0; j < mapsList.get(i).get(str).length; j++) {
                sum[j] += mapsList.get(i).get(str)[j];
            }
        }
        sumMap.put(str,sum);
    }

I hope that helped. 希望对您有所帮助。

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

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