• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Collection

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic