-2

I've got Treasure objects in a TreasureCollectionDB class. The TreasureCollectionDB class has a Map<Long, Treasure> (long being an id generated by the TreasureCollectionDB) called treasures and a second data collection/list (available treasures).

The thing I need the other Collection or List to do is hold Treasures which I will add/remove through JSP pages. The Treasures in this list should be unique, but sorted alphabetically (if there's no data holder that does this by it self, I will write a sort method). Anyone know what data holder I should use? Answers on the internet are confusing me as to which is most suitable.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

4

You may use TreeSet, that should give you the desired results. Set doesn't allow duplicates and Tree maintains sorted order. The Treasures may implement Comparable interface so that you can sort on the desired field(s).

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
0

You would need to create equals and hash code methods and also write a comparator. That comparator you may pass to a TreeSet and use SortedSet interface.

Emil Iakoupov
  • 189
  • 14