Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between hashmap 1.4 and 1.6

Tags:

java

hashmap

I have application which runs on both 1.4 and 1.6. Order of elements is getting printed in 1.6 version is different than 1.4. I used LinkedHashMap which fixed the problem. Now this order is exactly same as of 1.4. I didn't find 1.4 source code of HashMap except downloading entire jdk . Except generic whats been changed in newer versions?

like image 988
Delta Avatar asked Sep 01 '26 16:09

Delta


2 Answers

The iteration order of elements in HashMap was never guaranteed, so implementations were free to change it. This shortcoming has been remedied by LinkedHashMap.

Such a change might be caused by e.g. the complete revamping of the hashing algorithm in Java 1.4.1... but then again, we also noticed such a change upon migrating our project to Java6 so there was a change in that version too.

like image 148
Péter Török Avatar answered Sep 04 '26 07:09

Péter Török


The order of iteration in HashMap is undefined. That means it can (and does) change all the time. It depends not only on the code, but on the values of the hash codes of objects, which themselves can differ not only from version to version, but even from run to run.

LinkedHashMap exists specifically as a HashMap alternative with a predictable iteration order.

like image 26
Ernest Friedman-Hill Avatar answered Sep 04 '26 07:09

Ernest Friedman-Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!