I have an ArrayList, a Collection class of Java, as follows:
ArrayList animals = new ArrayList();
animals.add(\"bat\
If you use Eclipse Collections, you can use a Bag. A MutableBag can be returned from any implementation of RichIterable by calling toBag().
MutableList animals = Lists.mutable.with("bat", "owl", "bat", "bat");
MutableBag bag = animals.toBag();
Assert.assertEquals(3, bag.occurrencesOf("bat"));
Assert.assertEquals(1, bag.occurrencesOf("owl"));
The HashBag implementation in Eclipse Collections is backed by a MutableObjectIntMap.
Note: I am a committer for Eclipse Collections.