I have a class e.g. MyDataClass.
I have a HashMap<String,MyDataClass> myMap;
For each reference in the myMap, I also have some other MyDataClass references to the same object. To be specific:
MyDataClass c = new MyDataClass();
//configure c object with appropriate values
myMap.put("akeyvalue",c);
MyDataClass ref = c;
At this point ref and myMap.get("akeyvalue") refer to the same object in memory.
If I later do myMap.remove("akeyvalue") the entry in the hashmap will be removed, but the object ref will still refer to the same location. Essentially the value myMap.get("akeyvalue") still exists. How can I update it to null, so that it is synchronized to the hashmap? I.e. If I have distinct data structures/collections that refer to the same objects, what is the best pattern/design to use so as to keep them synchronized? In my small example, in the code that I do remove on myMap, I have no access to ref object. I must somehow find ref and null it. Generally, I could have a distict data structure e.g. List<MyDataClass> refs;
Thank you
You’ll have to do this manually. If you want a
remove(..)on one collection to affect another:HashMap(or any collection)private Collection anotherCollectionwith setter and getter in your classremove(..), callsuper.remove(..)and also callanotherCollection.remove(..)