• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why HashMap is maintaining sequence upto certain Range  RSS feed

 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • 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

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would appear to be one of the things that do not matter: http://www.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.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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 !
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Agreed. I was not going to increase the initial size. Was just curious about it.

Thanks !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!