• 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

How to sort HashMap in ascending order by default?

 
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've coded to sort HashMap(but HashMap doesn't preserve any order by default) in ascending order with TreeMap based on the Keys
HashMap sorted in ascending order by default. How it works for me? Then what's the use of TreeMap Class here?


[b]OUTPUT[\b]

Key : 2 Value : vino

Key : 3 Value : kishore

Key : 5 Value : praveen

Key : 6 Value : Mano

Key : 7 Value : ajith

Key : 9 Value : vino

AFter Sorting

Key : 2 Value : vino

Key : 3 Value : kishore

Key : 5 Value : praveen

Key : 6 Value : Mano

Key : 7 Value : ajith

Key : 9 Value : vino

and i m having few doubts? Please anyone Clarify my doubts
1.What is entry set?
2. What is Map.Entry?
HashMap-sorting-output.png
[Thumbnail for HashMap-sorting-output.png]
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinoth vino wrote:
HashMap sorted in ascending order by default. How it works for me? Then what's the use of TreeMap Class here?



One example doesn't make a conclusion. Try different keys, preferable some with much bigger integer values.

Henry
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the oracle documentation of tree map here. In case you dont
pass comparator it sorts the keys in natural order. So if key is Integer then it is ascending order, if string then alphabetic order.
 
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to find out how a Map works behind the scenes. It uses
h & c - 1
to find which bucket to put the Map.Entry into. That is a bit like a remainder operation but only works correctly if c is an exact power of 2 and never returns a negative result. So it is exactly right for finding array indices. Instead of putting
5, "Praveen"
try
0x15, "Praveen"
or
0x0000_fff5, "Praveen"
or
0xffff_fff5, "Praveen"
You will probably find all of those (except the last maybe) in the same bucket.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinoth vino wrote:and i m having few doubts? Please anyone Clarify my doubts


I'll try. You cannot order a HashMap (at least not without "trickery") because that's not what it was designed for. It is designed to find a specific key quickly - generally in close to O(1) time - via its hashCode().

There are, however, variants of HashMap - like LinkedHashMap - that can maintain entries in "addition" order; so if you sort before you add them, then they'll be in sorted order. However, as soon as you add another one they probably won't be.

There is also a variety of hashcode called a "bloom filter" which might be able (within limits) to do what you want, but it still won't work with java.util.HashMap, because that class does some more bit-crunching on your hashcode before it actually adds it to its internal structure.

So basically, the answer to the question in your subject line is: You can't.

HIH

Winston
 
What's that smell? Hey, sniff 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