views:

78

answers:

4

Hi,

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 refs;

Thank you

A: 

You'll have to do this manually. If you want a remove(..) on one collection to affect another:

  • extend HashMap (or any collection)
  • make a private Collection anotherCollection with setter and getter in your class
  • override remove(..), call super.remove(..)and also callanotherCollection.remove(..)`
Bozho
A: 

Um I think you have some incorrect ideas driving your question. Firstly after you have removed "akeyvalue" from the collection, you cannot access it again through the map. It does not matter whether there is another variable referencing the original value or not.

Secondly, if you as the developer wish to keep multiple references to the same object, it is up to you to design the software to clear all references to it, reducing it's reference count to zero and allowing the garbage collector to reclaim the memory. For example, having a reference to the same object in a map of available objects and a map of used objects is a practical example of multiple maps. You must maintain them.

In you example, you would not need to clear ref. Simple because it will be automatically cleared at the end of the current methods. Doing what you are asking is what we do in things like objective c iphone development, but unnecessary in java development.

Derek Clarkson
@Derek: I know I can not access it again through the map. I just want to update ref to reflect that the specific object is gone for good. Also ref is global. I just made a trivial example of the concept. My problem is that I have one collection (centralized) that gathers specific values, and another collection that references some of these values for some other processing. I just want these collections to be in sync
A: 

"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?"

I believe that the answer is encapsulation. If you keep your related data structures under one class with a clear API, then any action available by the API can update them and keep them synchronized. These data structures should be hidden; this way you guarantee that any access to them is controlled by the encapsulating class.

Eyal Schneider
A: 

I would agree with Bozho - subclass HashMap and add a way to add listeners to your new class. Override clear() and remove() to call an onDelete(Object o) in your listener.
When other objects wanted to know when something was deleted would simply register a listener with your HashMap and implement the onDelete(Object o) method and then presto they would be notified.

Romain Hippeau