lundi 22 juin 2015

Get the Distint and count values from a field of an object in a Collection

I have a List of Pin objects List and the Pin object has the fields

String pinNumber, String pinType, Date insertDate

I would like to get a HashMap with that have the distinct pinNumber telling me how many distinct pinNumber are in the List and a count of each .

So the way I know of to do this is to

Iterate throught the List Check if the HashMap contains 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?? Any idea maybe Guava has something simpler?

Aucun commentaire:

Enregistrer un commentaire