Skip to content
Advertisement

HashMap has all the entries changed to last insert for every cycle

I need to insert a key and an element, containing the key and another value (which will be selected in the end from the map sorted by keys) into an HashMap. I decided to lastly convert the HashMap to a TreeMap to sort it, but the HashMap gets filled with data that change at every insert:

here is the code

JavaScript

The output is something like this:

JavaScript

Advertisement

Answer

You need to create a new Element for every put. You can’t just reuse the same one.

Move

JavaScript

into the while loop.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement