Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic