简体   繁体   中英

Difference between hashmap 1.4 and 1.6

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?

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 eg 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.

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.

I suppose the implementation of HashMap has changed. HashMap doesn't guarantee any order, so you shouldn't rely on that, because the could change it with new releases without any notice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM