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


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Map Family" Watch "Map Family" New topic
Author

Map Family

ajaysharma fbd
Greenhorn

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
Bartender

Joined: Feb 26, 2001
Posts: 6294
    
  15

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
Bartender

Joined: Sep 20, 2010
Posts: 3995
    
  18

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.
 
Have you checked out Aspose?
 
subject: Map Family
 
It's not a secret anymore!