I would like to know a way to implement a scenario that I am trying to do.
I have a HashSet of Objects with Object having an attribute A of type int. Now, I want a representative from each of the distinct partitions of HashSet based on the distinct values of the attribute A.
For example:
Let the HashSet be
{O1, O2, O3, O4, O5, O6} where O1 to O6 are objects with attribute values as follows
Partition #1
O1.A = 1
O2.A = 1
Partition #2
O3.A = 2
O4.A = 2
O5.A = 2
Partition #3
O6.A = 3
I want a representative from each partition,
e.g. {O1, O4, O6}
Can anybody suggest some way to do this? Any help will be appreciated.
Thanks,
Somnath
Make a
Map<Integer, YourClass>(probably best a TreeMap, so the partitions are in order), and copy the data into it:After this,
partitionscontains one entry for every partition found, with one representative each (the first one found).