How to count the number of occurrences of an element in a List

后端 未结 22 1507
一生所求
一生所求 2020-11-22 12:25

I have an ArrayList, a Collection class of Java, as follows:

ArrayList animals = new ArrayList();
animals.add(\"bat\         


        
22条回答
  •  故里飘歌
    2020-11-22 12:29

    I'm pretty sure the static frequency-method in Collections would come in handy here:

    int occurrences = Collections.frequency(animals, "bat");
    

    That's how I'd do it anyway. I'm pretty sure this is jdk 1.6 straight up.

提交回复
热议问题