Limeran
Limeran

Reputation: 193

Retrieving value from an object within a Map

I am writing an interpreter program and I am stuck at the moment with this. There is a Map for Integers and MJObjects:

private Map<Integer, MJObject> objectHeap;

objectHeap = new HashMap<Integer, MJObject>();

MJObject class looks like this:

MJObject(SymbolTable symTab, String className)

I create a new MJObject and store it inside a Map with a reference integer.

public Integer allocClassInstance(String className)
MJObject object = new MJObject(symTab, className);
objectHeap.put(nextFree, object);

Then from another method using just the reference of the MJObject, I need to retrieve the className inside the MJObject. How can I do that? Thank you for your help.

Upvotes: 0

Views: 97

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499880

Then from another method using just the reference of the MJObject, I need to retrieve the className inside the MJObject. How can I do that?

If you've already got the MJObject then the map is irrelevant. Assuming the MJObject makes the class name it was constructed with accessible somehow, you just want something like:

String className = mjObject.getClassName();

If you're actually trying to get the key in the map which is associated with that MJObject, you'd have to iterate through the map - or potentially create a second map with the reverse mapping (MJObject to Integer).

Upvotes: 2

Related Questions