Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Collection

 
Vishal Methi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interface you want is SortedMap. The only implementation of this in the JDK is TreeMap (both in java.util).
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jacquie Barker
author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic