aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is the hashmap in java not sorted? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is the hashmap in java not sorted? " Watch "why is the hashmap in java not sorted? " New topic
Author

why is the hashmap in java not sorted?

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Hello ranchers,
its great to be here after so long

a question is coming to my mind
"why is the hashmap not sorted? "

I know that tree map is provided for sorting but
still I want to know why hashmap was made such that it does not sort ....

were there any technical problems? or some limitations?

please enlighten


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

I dont know why its so, the designers would have a answer.
But my take is: Hashmap is mainly used for storing key-value pairs and there is no intention of keeping the order- be it insertion order or some sorting order. So that way Hashmap is kept simple, if one wants more features choose a better data structure (Collection)


Mohamed Sanaulla | My Blog
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Prasad Kharkar wrote:
...
still I want to know why hashmap was made such that it does not sort ....
...


As a user of Hashmap do you really care for their order? You are mostly concerned with getting your data in O(1) time.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Mohamed Sanaulla wrote:As a user of Hashmap do you really care for their order? You are mostly concerned with getting your data in O(1) time.



Agreed. BTW, is it even possible to sort any set of generic objects, and still maintain an O(1) ? Unless someone invented something new recently, I don't think so.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why is the hashmap in java not sorted?