John
John

Reputation: 2852

Which Collection would do this?

I am reading following data -

Year, No, Name, 
2009, 1, ABC
2009, 2, PQR
2009, 3, XYZ
2010, 1, BCD
2010, 2, DEF
2010, 3, JKL
2011, 1, FGH
2011, 2, IJK
2011, 3, LMN

Its a sample, there are thousands of these type. I need to display the unique Year value in JList and for the selected Year, I have to give a CheckBox, which if selected, would display the Name sorted on No, otherwise sorted on Name.

Upvotes: 1

Views: 101

Answers (3)

Rostislav Matl
Rostislav Matl

Reputation: 4543

I suppose you need the years sorted too, so as top-level use TreeMap with year as key, the value will be your class wrapping two TreeSets - on for No, one for Name - they will be sorted just by adding values to it, plus they do not allow duplicates.

Or do not create the wrapping class and use two TreeMaps - this is not a big difference.

Upvotes: 1

Artem
Artem

Reputation: 4397

Map<String, Set<YourStruct>> 

with year as a key? YourStruct - bean, holding year, no and name. You can use Collections.sort() for sorting.

Upvotes: 2

wyz
wyz

Reputation: 761

What you need looks like a Map, key will be the Year, and value will be a list of No and Number (you need to create your own class).

Then you need to create two Comparator, one is based on Name, the other is No. So when checkbox is checked, you use Name-based comparator to sort the list, when unchecked, use No-based comparator to sort the list.

Upvotes: 3

Related Questions