简体   繁体   English

hashmap 1.4和1.6之间的区别

[英]Difference between hashmap 1.4 and 1.6

I have application which runs on both 1.4 and 1.6 . 我的应用程序运行在1.41.6 Order of elements is getting printed in 1.6 version is different than 1.4 . 元素的顺序是打印在1.6版本不同于1.4 I used LinkedHashMap which fixed the problem. 我使用了LinkedHashMap修复了这个问题。 Now this order is exactly same as of 1.4 . 现在这个订单与1.4完全相同。 I didn't find 1.4 source code of HashMap except downloading entire jdk . 除了下载整个jdk之外,我没有找到HashMap 1.4源代码。 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. HashMap中元素的迭代顺序从未得到保证,因此实现可以自由地更改它。 This shortcoming has been remedied by LinkedHashMap . 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. 这样的改变可能是由于例如Java 1.4.1中的哈希算法完全改进 ......但是,我们还注意到在将项目迁移到Java6时发生了这样的改变,因此该版本也发生了变化。

The order of iteration in HashMap is undefined . HashMap的迭代顺序是未定义的 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. LinkedHashMap特别作为具有可预测迭代顺序的HashMap替代方案存在。

I suppose the implementation of HashMap has changed. 我想HashMap的实现已经改变了。 HashMap doesn't guarantee any order, so you shouldn't rely on that, because the could change it with new releases without any notice. HashMap不保证任何订单,因此您不应该依赖它,因为可以在没有任何通知的情况下使用新版本更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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