How to count occurrence of an element in a List
- by MM
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.