I have the following declaration in class A:
Map<String, MyClass> myMap = Collections.synchronizedMap(new LinkedHashMap<String, MyClass>());
Class A also has this function:
public List<MyClass> getMapValuesAsList() {
return new ArrayList<>(myMap.values());
}
In class B, I have a List initialized like this:
List<MyClass> myList = cart.getMapValuesAsList();
What I assume is that myList keeps references to the values of myMap. When I call a setter function in an item in myList, the related value is also updated in myMap, supporting my assumption. However, whenever I delete an item from myList, myMap continues to keep the MyClass instance in it. This removed instance is probably not garbage collected since myMap still has a reference to it. Am I right?
Is there any automated way to delete key-value pair from map when the value is removed somewhere else?
UPDATE: Class B pass myList to an Android adapter. Therefore, I need some kind of get functionality. Collection doesn't have such a method. Any recommendations?