Count the occurrences of items in ArrayList

后端 未结 6 829
长发绾君心
长发绾君心 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:35

    Thanks for your all nice suggestion. But this below code is really very useful as we dont have any search method with List that can give number of occurance.

    void insert(Item newEl) 
    { 
       if (counters.contains(newEl)) 
         counters.put(newEl, counters.get(newEl)+1); 
       else 
         counters.put(newEl, 1); 
    
       items.add(newEl); 
     } 
    

    Thanks to Jack. Good posting.

    Thanks,

    Binod Suman

    http://binodsuman.blogspot.com

提交回复
热议问题