简体   繁体   中英

HashMap Inside of ArrayList in Java geting Over Write

Suppose I have a String "abca" . I want to keep track of each alphabet occurrence in every index using HashMap & ArrayList in Java. For example if a=0,b=1,c=2, I want the output like below: for input: abca

[{0=1},{0=1, 1=1},{0=1, 1=1, 2=1},{0=2, 1=1, 2=1}];

I have written this solution in Java:

public static void main(String[] args){
     Scanner in = new Scanner(System.in);

     String x = in.next();
     char a[] = x.toCharArray();
     HashMap<Integer,Integer> hm = new HashMap();
     ArrayList<HashMap<Integer,Integer>> list = new ArrayList();

     for(int i=0;i<a.length;i++){
         if(hm.get((int)a[i]-97)==null){
             hm.put((int)a[i]-97, 1);
         }
         else{
             int pow = hm.get((int)a[i]-97);
             pow++;
             hm.put((int)a[i]-97, pow);
         }
         list.add(hm);
        // System.out.println(list);
     }
     System.out.println(list);
}

But I am getting output as:

[{0=2, 1=1, 2=1}, {0=2, 1=1, 2=1}, {0=2, 1=1, 2=1}, {0=2, 1=1, 2=1}]

At the end of the iteration, ArrayList is updating all the indices of the last stage of HashMap.

The original map will be mutated in every iteration, so to fix this you need to create a new HashMap and add it in every iteration.

list.add(new HashMap<>(hm));

output:

[{0=1}, {0=1, 1=1}, {0=1, 1=1, 2=1}, {0=2, 1=1, 2=1}]

Well, if you're interested, you can also do it like this.

         Scanner in = new Scanner(System.in);
         String x = in.next();
         char a[] = x.toCharArray();
         List<HashMap<Integer, Integer>> list = new ArrayList<>();
         HashMap<Integer, Integer> hm = new HashMap<>();

         for (char c : a) {
             hm.compute(c-'a', (k,v) -> v == null ? 1 : ++v);
             list.add(new HashMap<>(hm));
         }

         System.out.println(list);

The hm.compute() method looks for the value for the supplied key.

  • if null, it initializes to 1.
  • otherwise it increments the value by 1

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