简体   繁体   中英

java adding sublists into the nested hash maps in for loop

I have a multidimensional array whose name banksNumberOfOrders which is initialized with some integer values.

I have builted nested hashmap that have two integer keys which are also row and column indexes of banksNumberOfOrders and have one list.

I am adding random values with number of banksNumberOfOrders[i][j] into the list by nested for loops.And, also I add sublists of this list into the nested hashmap by same nested for loops.

The code which I write is such as

Map<Integer, Map<Integer,List<Integer>>> paymentOrderAmounts = new HashMap<Integer, Map<Integer,List<Integer>>>(); 
List<Integer> amounts = new ArrayList<Integer>();

int count_begin = 0; //list size
for (int i=0; i<n; i++){
    for(int j=0; j<n; j++){
        if(i != j){
            for(int a=0; a<banksNumberOfOrders[i][j]; a++){         
                int amount = ThreadLocalRandom.current().nextInt(begin1, end1);
                amounts.add(amount);
            }   
            int count_end = count_begin+banksNumberOfOrders[i][j];          
            paymentOrderAmounts.put(i, new HashMap<Integer,List<Integer>>());
            paymentOrderAmounts.get(i).put(j, amounts.subList(count_begin, count_end));
            count_begin = count_end;
            System.out.println(i+" --> "+j+" "+paymentOrderAmounts.get(i).get(j)+" ");
        }   
    }       
    System.out.println();
}

This code is running without error and printing values. But when I try to print the values of nested hashmap, I am giving the concurrentmodificationexception because of sublist usage. Second code piece which causes error such as

//traverse hashmap and print values
for(Map.Entry<Integer, Map<Integer,List<Integer>>> t:paymentOrderAmounts.entrySet()) {
           Integer key = t.getKey();
           for (Entry<Integer, List<Integer>> e : t.getValue().entrySet())
             System.out.println(key + "-->" + e.getKey()+" "+e.getValue());
        }

According to my google searches , I saw that I have to use iterator to add element into list to throw off error , otherwise error occurs. But I dont understand that I already using loop , how can I use iterator in this nested loop ?

If you are doing things in a multithreaded fashion, using a java.util.concurrent.ConcurrentHashMap rather than a java.util.HashMap might be important. See also the java.util.concurrentConcurrentSkipListMap as an alternative that will iterate in a less random ordering of the keys.

To traverse the map,

for(Integer outerKey: paymentOrderAmounts.keySet()) {
     Map<Integer, List<Integer> innerMap = paymentOrderAmounts.get(outerKey);
     for (Integer innerKey: innerMap.keySet()) {
         List<Integer> innnerList = innerMap(innerKey);
         for (Integer innerValue: innerList) { 
             System.out.println(outerKey + "-->" + innerKey +" "+innerValue);
         }    
     }
}

may work better, since it isn't trying to grab a concurrently changing EntrySet and just works with one item at a time.

        Table<Integer, Integer, List> paymentAmounts = HashBasedTable.create();
        List<Integer> amounts = new ArrayList<Integer>();
        int count_begin = 0; //list size
        for (int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(i != j){
                    for(int a=0; a<banksNumberOfOrders[i][j]; a++){         
                        int amount = ThreadLocalRandom.current().nextInt(begin1, end1);
                        amounts.add(amount);
                    }   
                    int count_end = count_begin+banksNumberOfOrders[i][j];          
                    paymentAmounts.put(i,j,amounts.subList(count_begin, count_end));
                    count_begin = count_end;
                    System.out.println(i+" --> "+j+" "+paymentAmounts.get(i, j)+" ");
                }   
            }       
            System.out.println();
        }

        System.out.println(paymentAmounts.cellSet()); //gives error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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