• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why HashMap is maintaining sequence upto certain Range

 
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This question might have been asked so many times. But i did not get satisfactory answer. I am trying to store certain objects in HashMap with Integer as key and Student Bean as value. I know HashMap use hashing to store entries. I ran below code lot of times and found sequence is maintained till element 15. After that entries are stored randomly. Is it because HashMap initial capacity is 16 and it tries to rehash the element after re-sizing and hence rest of elements are stored in random order?




output:
1 2 4 5 6 8 9 10 11 12 13 14 15 17 16 19 18 88

 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This would appear to be one of the things that do not matter: https://coderanch.com/how-to/java/DontSweatIt. Generally I'd say curiosity is good, but in this case the answer is simply "because the developers of whatever JRE implementation you're using implemented it that way". If it's the Oracle JRE, then the source of the class libraries comes with the JDK, and you can check out what exactly is happening. But my advice is: don't. There are many more important things to learn about Java.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you want to keep insertion order use LinkedHashMap. But I guess you already know that ;).
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer and Pawel Pawlowicz ... Yes i know its the implementation thing. I have to send some sequence of big range to third party through my code and I cannot change it to LinkedHashMap since it is again third party jar file i am using. I think the only solution left is apply Comparator in my class to sort it.
Thank you both !
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the third party jar requires you to put a HashMap? Not just a Map?
And that's why you should program to interfaces!

What if you create a subclass of HashMap that delegates all methods to private field of type LinkedHashMap ;)?


EDIT: I just realized that what I wrode just does not make sense as LinkedHashMap already is a subclass of HashMap!
So why are you not allowed to use a LinkedHashMap and you are allowed to use HashMap?
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot do that... I am using that method from jar file which returns me the entries Map with random order. I saw the implementation is done using HashMap. And My class is not the Child class that Class in jar file that i can override the method.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I understand it right that your problems is with the HashMap you obtain from the third-party library? If it is so, it's completely up to you what you'll do with the result. You might create a TreeMap from the HashMap using the comparator you desire and use just the TreeMap thereafter.
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Martin, I did the same as i mentioned in my earlier comments. This post was for understanding the reason why elements are stored in random in HashMap after element 15 not before that. Is it because of initial size of HashMap? Ulf Dittmer mentioned already that its the implementation done like that.

Thanks for your valuable suggestion !
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Surinder Mehra wrote:I cannot do that... I am using that method from jar file which returns me the entries Map with random order. I saw the implementation is done using HashMap. And My class is not the Child class that Class in jar file that i can override the method.


Like the others, I'm a bit mystified by your requirements. Is it the third-party method that requires these mappings in a specific order, or you?

If it's you, then plainly you're going to have to arrange them yourself (and a few options have been given); if it's your 3P, and it takes a HashMap, then use a LinkedHashMap and supply that.

I suspect you may be overthinking this.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Surinder Mehra wrote:yes Martin, I did the same as i mentioned in my earlier comments. This post was for understanding the reason why elements are stored in random in HashMap after element 15 not before that. Is it because of initial size of HashMap?


Very possibly, but at the end of the day, if the Map is NOT in the order you want, does it really matter? Trying to create a "solution" by increasing the initial size is only likely to lead to disaster.

Winston
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. I was not going to increase the initial size. Was just curious about it.

Thanks !
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic