• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23919
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

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
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic