Skip to content
Advertisement

HashMap element is removed when another HashMap name remove an element

appearList is a HashMap with fixed data. I do:

JavaScript

Then, when I remove an element from randomPersonList, that element on appearList is also removed unexpectedly. I expect to keep appearList as original.

Can anyone tell me why and how to correct my code?

JavaScript

Advertisement

Answer

JavaScript

makes randomPersonList the reference to the same object as appearList. So with both variables you are accessing the same object. Either create two HashMaps or create a clone.

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