How to count occurrence of an element in a List

Posted by MM on Stack Overflow See other posts from Stack Overflow or by MM
Published on 2009-02-03T03:29:13Z Indexed on 2010/03/17 3:41 UTC
Read the original article Hit count: 320

Filed under:
|
|
|

I have an ArrayList a collection class of java as follows.

ArrayList<String>animals = new ArrayList<String>();
animals.add("bat");
animals.add("owl");
animals.add("bat");
animals.add("bat");

As you can see the animals ArrayList consists of 3 bat elements and one owl element. I was wondering if there is any API in collection framework that returns the number of bat occurrences or is there a way to determine number of occurrences.

I found that google's collection multiset does have an api that returns total number of occurrences of an element. But that is compatible only with jdk1.5. Our product is currently in jdk 1.6. Hence cannot use it.

© Stack Overflow or respective owner

Related posts about java

Related posts about collections