For example, say I have an ArrayList that could contain the following values:
x
x
x
y
y
Now what I want to retrieve is the number of x and x and I want to be able to differentiate what I have, either x or y because in actuality, I could have any object in the ArrayList and I have to be able to tell them apart.
What I was thinking of doing was first converting the ArrayList into a LinkedHashSet, which would keep the ordering and remove the duplicates so I would just have x and y But how would I get the number of each and associate it with the proper element?
Overall what I want to do is to be able to write a toString method that will let me output:
x3y2
But without knowing that x and y are the elements because they could have been something else like z or w.
Check out the Multiset in google guava: http://docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/collect/Multiset.html
Example:
More examples can be found on the Guava wiki.