Major Java HashMap Issues

I am currently creating my own serializer as part of the project, and here I need to serialize the links as well, which means that if two serializers refer to the same project before serialization, they should also after deserialization.

To do this, I use HashMap, matching each object with an identifier that I can use as an attribute for each serialized object, and at the same time keep track of which objects are already serialized. However, I came across some problems.

It seems that java HashMap only checks for equality on keys, and not on the fact that they are the same reference. For example, in the future, the variable b ends:

HashMap<Object, Integer> map = new HashMap<Object, Integer>();
LinkedList<Object> ll1 = new LinkedList<Object>();
LinkedList<Object> ll2 = new LinkedList<Object>();
map.put(ll1, 5);
boolean b = map.containsKey(ll2);

Several times the result does not make sense. B is also true here:

HashMap<Object, Integer> map = new HashMap<Object, Integer>();
LinkedList<Object> ll1 = new LinkedList<Object>();
Stack<Object> stack = new Stack<Object>();
map.put(ll1, 5);
boolean b = map.containsKey(stack);

, ContainsKey equals, '==' -, , . ?

+4
1

, , , a.equals(b). == , equals .

, LinkedList Stack , , List, List.equals , , . , "".

, IdentityHashMap. , ==, Object.equals() . , - System.identityHashCode Object.hashCode().

+5

All Articles