Difference between ArrayList and HashSet and memory leak in java

The collection
ArrayList can store duplicate elements
HashSet cannot store duplicate elements The equals method is used to determine whether they are equal. Usually, the hashcode and the equals method can be modified by memory address conversion. The
hashcode is valuable, and the storage collection must be
a object is stored In the hashset collection, the hashcode value is obtained by field conversion and stored in a specific memory location.
Once the value of the field in the object is modified, the hashcode is re-converted, and the hashcode value changes.
When you want to remove the modified object and perform the remove operation on the collection , it will find the location of the object according to the new hashcode to remove.
Since the object has been stored in the memory according to the original hashcode value, the original object cannot be found through the new hash value. So once the value of the field is modified, and the hashcode is converted by this field
, the object cannot be removed. cause a memory leak.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324641966&siteId=291194637