Count the occurrences of items in ArrayList

后端 未结 6 844
长发绾君心
长发绾君心 2020-12-19 13:18

I have a java.util.ArrayList and an Item object.

Now, I want to obtain the number of times the Item is stored in t

6条回答
  •  渐次进展
    2020-12-19 13:45

    I know this is an old post, but since I did not see a hash map solution, I decided to add a pseudo code on hash-map for anyone that needs it in the future. Assuming arraylist and Float data types.

     Map hm = new HashMap<>();
     for(float k : Arralistentry) {
     Float j = hm.get(k);
     hm.put(k,(j==null ? 1 : j+1));
     }
     for(Map.Entry value : hm.entrySet()) {
    System.out.println("\n" +value.getKey()+" occurs : "+value.getValue()+" times");
      }
    

提交回复
热议问题