• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap in JDK 5 Vs JDK 6 - a strange behavior!  RSS feed

 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I am facing a strange scenario when I run my java program across two different JVMs (one is of JDK5 and JDK6). The program uses a HashMap and the order in which the entries are stored in the HashMap does differ hence causing a great deviation in the output of the program. I do a calculation based on a business logic which determines the placement of values in the HashMap and returning a boolean (true/false) status accordingly to indicate the duplicate of values.

I noticed the change when I ran the program in two different environments producing two different results for the same program (one returns true and other returns false) and upon analyzing I found that the difference was only in the JRE versions.

I too agree and am aware that the order is not preserved and guaranteed in HashMap. But no matter how many times I run the same program, the output is same. The orders are maintained in the same way in each JRE.

However there are two alterations which yielded the same result :

  • When I tried altering the key sequence [by changing a different special character which is a separator/control character] so as to change the hash value for keys, the output is same for a particular run. .
  • Needless to say, the usage of TreeMap instead of HashMap helped in getting the consistent and same output in both the JREs.


  • Have any of you encoutered such scenarios? Any hints or insights?

    Thanks,
    Raghavan alias Saravanan M.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Of course, this particular link adds some value : http://jeremymanson.blogspot.com/2007/03/hashmap-behavior-in-jdk6.html
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I too agree and am aware that the order is not preserved and guaranteed in HashMap. But no matter how many times I run the same program, the output is same. The orders are maintained in the same way in each JRE.



    I always find this to be a very weird argument -- that everyone seems to always make. Why?

    Unordered doesn't mean that the order is random. Unordered doesn't mean that the order is different every time. Unorder simply mean that there is nothing in the code that enforces order. And that the code can be changed between versions of the JVM, for many reasons, and no care will be taken to make sure that the order is the same.

    Basically, "orders are maintained in the same way in each JRE" does not mean that the map is ordered. There is nothing in the JavaDocs that requires this.

    Henry
     
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm not sure why you would be surprised that something that is not guaranteed did indeed change between JRE versions.

    You'll find quite a few spots in the javadocs where it says something to the effect of "you can't rely on this or that always being the case". That means that Sun reserves the right to change the implementation at any given time if it suits them. Code that does rely on it often breaks if run on a different JRE version, or on a JRE from a different vendor. Sam thing if you use classes in sun.* and com.sun.* packages.

    Or are you surprised that it doesn't change between runs on the same version JRE? The algorithms being used are not stochastic (for the most part), so it would be surprising if the same inputs resulted in different ways of storing things.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry Wong wrote:

    Unordered doesn't mean that the order is random. Unordered doesn't mean that the order is different every time. Unorder simply mean that there is nothing in the code that enforces order. And that the code can be changed between versions of the JVM, for many reasons, and no care will be taken to make sure that the order is the same.

    Basically, "orders are maintained in the same way in each JRE" does not mean that the map is ordered. There is nothing in the JavaDocs that requires this.

    Henry


    Thank you Henry. Yes you are right. But my question was something different. I meant to ask "why the output seems to almost and same when being run in one JRE". I understand that it may change but not certainly. But I could never get any luck in getting a different ordering of values in the same JRE.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ulf Dittmer wrote:I'm not sure why you would be surprised that something that is not guaranteed did indeed change between JRE versions.

    You'll find quite a few spots in the javadocs where it says something to the effect of "you can't rely on this or that always being the case". That means that Sun reserves the right to change the implementation at any given time if it suits them. Code that does rely on it often breaks if run on a different JRE version, or on a JRE from a different vendor. Sam thing if you use classes in sun.* and com.sun.* packages.



    Yes Ulf. Thank you. I agree with that and of course the piece of text would definitely be highlighted be it bolded or underlined!


    Or are you surprised that it doesn't change between runs on the same version JRE?
    .

    There you are!! Very well, I mean to ask the same !

    The algorithms being used are not stochastic (for the most part), so it would be surprising if the same inputs resulted in different ways of storing things.


    Yes of course. But it looks like no way there is a change. Having said that the order is *not* guaranteed, atleast there can be a slight variation across several runs in a single JRE.

    But if you compare the outputs of two different JREs, they are almost always the SAME. Hence the doubt!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!