aspose file tools*
The moose likes Java in General and the fly likes Doubt in Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt in Collection" Watch "Doubt in Collection" New topic
Author

Doubt in Collection

Vishal Methi
Ranch Hand

Joined: Jan 14, 2004
Posts: 52
Hi frnds..
Hey i just need one small help. Could u please tell me that which collection object i have to use so that while retriving i can get the data in sorted order and just let me know suppose while retriving the data when it gives in sorted data is it dependent on the key of the map. I need the collection object in MAP interface.
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
The interface you want is SortedMap. The only implementation of this in the JDK is TreeMap (both in java.util).
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
and just let me know suppose while retriving the data when it gives in sorted data is it dependent on the key of the map


In case you want to sort the values and not the keys use the values() method in the Map implementaion to get the values collection and then
use Collections.sort() method to do the sorting
Vishal Methi
Ranch Hand

Joined: Jan 14, 2004
Posts: 52
Hey Thanx yaar...
Could u plz one example of it .. It'll be very much helpful for me ..

Thanx again

Vishal
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
If all you want to sort is a set of Strings, you can also use the TreeSet class, which is truly a Collection in the capital "C" sense of the word (note that the TreeMap class does not officially implement the Collection interface, but is a collection in the generic sense).

First, an example using TreeSet:



When run, the names will print in sorted order.

I'll be back in a moment with a TreeMap example!

J.


Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
Here's a TreeMap example; Maps store key-value pairs, so I'm using Strings as keys, Animal objects as values. (Note that we can use ANY type of Object for both the key or the value, but there's more work to do if we use an arbitrary type of object X as the key -- namely, we have to write additional code to define what it means for one instance of X to be less than/equal to/greater than another instance of X.)



Hope this helps!

Jacquie
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
Originally posted by Jacquie Barker:
If all you want to sort is a set of Strings, you can also use the TreeSet class ...


Just to clarify: a TreeSet can also sort Objects in general, not just Strings -- see my comments in the TreeMap example.

J.
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
This is in case you wanted to sort the values



use Collections.sort() method to do the sorting

sorry for that part, It should have been Arrays.sort()
[ July 15, 2005: Message edited by: Rajagopal Manohar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Collection