• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

why is the hashmap in java not sorted?

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!