Results 1 to 8 of 8
  1. #1
    aisha05 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default sorting a hash map without creating a new treemap

    Hi all,
    I have following questions:
    1)Is there any other way to sort a hashmap(based on key ordering) without using treemap?
    2)IF there is a way to sort a hashmap without creating a new instance of treemap,so it is consume lots of time as compare to sorting it with creating a new of treemap??

    Please help me to solve my puzzle.
    Thanks in advance:
    (happy):

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,801
    Rep Power
    8

    Default

    Sorting a Map makes no sense. Key/Value pairs are placed into a Map based on their hash not a particular order.

  3. #3
    aisha05 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Dint get you junky.

    But i want to sort my hashmap.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by aisha05 View Post
    Hi all,
    I have following questions:
    1)Is there any other way to sort a hashmap(based on key ordering) without using treemap?
    2)IF there is a way to sort a hashmap without creating a new instance of treemap,so it is consume lots of time as compare to sorting it with creating a new of treemap??
    You can store the keys of your Map in an ArrayList and sort that collection, but I'd store everything in a TreeMap right away.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    aisha05 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    so is it impact any performance time ??

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by aisha05 View Post
    so is it impact any performance time ??
    Erm, yes; the sorting is as fast as can be but, as I wrote, why not stick your entries in a TreeMap in the first place?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    aisha05 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    ohh. so it means if i stored my map in treemap so it is much faster than sorting its key??

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by aisha05 View Post
    ohh. so it means if i stored my map in treemap so it is much faster than sorting its key??
    No, storing n entries in a HashMap takes O(n) steps; storing them in a TreeMap takes O(n*2log(n)) steps; sorting n items also takes O(n*2log(n)) steps. So, as I wrote, store those entries in a TreeMap in the first place.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. TreeMap vs Map
    By alpdog14 in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 06:07 PM
  2. [JAVA] TreeMap
    By watle in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2011, 06:39 PM
  3. Treemap's
    By Russo in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 01:13 AM
  4. Sorting Elements in a TreeMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:47 PM
  5. How to use treemap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •