It's not a secret anymore!
The moose likes Beginning Java and the fly likes Map Family Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Map Family" Watch "Map Family" New topic

Map Family

ajaysharma fbd

Joined: Aug 06, 2012
Posts: 2
why we don't get order in HashMap.
I know it uses the hashcode but what differnce did it make to the order.
Here By order I mean elements that are added first to the HashMap should be added first I am not talking about sorting.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6319

The implementation is based on a hash function, which is favorable for speed. Sorting/ordering is a different concern that limits the ability to make things work fast. HashMap favors speed. If you need to preserve order, use a SortedMap implementation.

Junilu - [How to Ask Questions] [How to Answer Questions]
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4087

EnumMap and LinkedHashMap are also ordered. SortedMap also keeps elements sorted though.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: Map Family
jQuery in Action, 3rd edition