Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SortedMap

Status
Not open for further replies.

thebarslider

Programmer
Dec 21, 2001
80
GB

I have some objects i am retrieving from a database and wish to order these on a particular String contained within the object. I.e. Category. I can do this using a comparator object but this removed the duplicate values that i need.

Does anyone have any advice? Maybe i am going about this in the wrong way?

Any help would be much appreciated.

Mark.
 
I don't understand why the comparator removes duplicate values.

Take a look at sort method on java.util.Arrays class.

Cheers,

Dian
 
From the javadocs:
A Map:
An object that maps keys to values. A map cannot contain
duplicate keys; each key can map to at most one value.

and SortedMap:
Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if the sorted map is to correctly implement the Map interface.

So a Map may not contain two equal objects, and if your comparator decides, two objects are equal (because the 'category'-Attribut is equal - while other attributes differ) they are rejected to be inserted.

You comparator could decide on other attributes that the elements aren't equal, after comparing the category.



seeking a job as java-programmer in Berlin:
 
A map can only have one entry per key value, the map uses the keys for lookup, the best way to get arround this is to use chaining in the case of multipule objects with identical keys... Or to use a different data structure.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top