• 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

The use of TreeMap

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ranchers,
while i was studying chapter 7 @ K&B SCJP book, a question came up.

I am playing around with Maps, Sets and Lists.
So far i dont get the use of class TreeMap.
Maps = key based collections
TreeXXX = sorted

While i am able to create a sorted Map by using TreeMap i wonder what is the advantage compared to a unsortet Map.
TreeMap and HashMap got the same methods to access the values. They are key based. So why should i care the order? Unlike TreeSet i am not able to iterate through it.

Best regards,
Frank Hinkel
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not able to iterate ? Look at the SortedMap API, you will see that there are ordering related methods in there. For example, here is a Copy/Paste from SortedMap (which TreeMap implements) :
This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods).
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you want that element should be stored in sorted order so that when you will access the element they can be accessed in eitherv ascending or decsending order
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can refer to http://forum.java.sun.com/thread.jspa?threadID=488125&messageID=2290289 for TreeMap vs HashMap
 
Frank Hinkel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i did. Well i actually can iterate through it by converting into a Set. Anyway i consider about the use. It is a bit elaborte to get a sorted collection with dublicates.

Mhm, i dont like TreeMap so far.
 
Frank Hinkel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Jiang:
You can refer to http://forum.java.sun.com/thread.jspa?threadID=488125&messageID=2290289 for TreeMap vs HashMap



Thanks, that is exactly what i am looking for.
[ April 30, 2008: Message edited by: Frank Hinkel ]
 
James Jiang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome
 
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic