I have a List of Pin objects (List<Pin>) where the Pin class has the following attributes:
String pinNumber, String pinType, Date insertDate
I would like to get a HashMap with <String pinNumber, int count> that have the distinct pinNumber telling me how many distinct pinNumber are in the List<Pin> and a count of each.
So the way I know of to do this is to:
- Iterate through the
List<Pin> - Check if the
HashMapcontains already the key value of the pinNumber and: - Increase it or add it if it does not exist.
I would like to do the same for each of the fields from the Pin object.
I am sure there should be an easier way to do this?
Maybe Guava has something simpler?
Here is one possible solution if you didn't want to rely on another library and wanted to maintain backward compatibility with older JVMs. It isn't the best, or the easiest to use, but it does work.
FrequencyUtil.javaFrequencyTest.javaPin.javaoutput
Just for fun and historical reference, a Java 1.2 version:
FrequencyUtil12.java