3

I'm doing some coursework for uni, I really should know this but I am unsure how I can update an object stored in a HashMap.

I have an abstract 'User' class that extends into 'Customer' and 'Staff' classes, instances of which are stored in a HashMap named 'mapUsers'.

The way I was thinking it could be done is saving the element to be modified into a temp 'User' object, on this temp instance I could modify the Object in any necessary way.

My real question is, will this update the object stored in the HashMap or will I have to remove the element stored in the HashMap and replace with the modified temp instance.

Is there an easier way to do this, I thought maybe something like

HashMap.get(index).performOperation();

something like that, where I can perform an operation without removing elements.

1
  • For the user HashMap it is a UserID, A String. And for the Account HashMap, an account number of type int. Commented Nov 19, 2011 at 16:08

3 Answers 3

17

Since your HashMap holds references, doing this:

Person p = new Person();
p.setName("John");
hashMap.put(1, p);
p.setName("Jack");

will change the name also inside the HashMap, because both references point to the same thing.

Or alternatively, assuming p is already in the HashMap:

Person p = hashMap.get(1);
p.setName("Jack");
Sign up to request clarification or add additional context in comments.

8 Comments

And so will hashMap.get(1).setName("Jack")', which is what I believe the OP was looking for.
I add the objects into the appropriate HashMaps when the program starts. After I've added the objects though, I dont have a way to reference it other than it's key?
Well if you don't keep the initial references, then no. The point of a HashMap or any hash table for that matter is to use the key to quickly identify a value.
Sorry I was a bit confusing there. I do have the key. "hashMap.get(1).setName("Jack")" Is exactly the kind of thing I needed. That resolves all ambiguity, I just couldn't find an example like that online! Thanks guys!
I couldn't access where the objects were instanciated, for example Person p = new Person(); HashMap.put(1,p); was done inside a method elsewhere.
|
2

What I will do in this kind of case is - try it out. In short, what you got is a reference not a value, so changes made to the reference will be reflected in the collection.

import java.util.*;

public class Test {
    public static void main(String args[]) {
        Test test = new Test();
        test.letsSee();
    }

    public void letsSee() {
        List<Thing> things = new ArrayList<Thing>();
        things.add(new Thing(1));
        Thing thing = things.get(0);
        thing.i = 10;
        for (Thing t : things) {
            System.out.println(t.i);
        }
    }

}

class Thing
{
    public int i;

    public Thing(int i) {
        this.i = i;
    }
}

Comments

1

Yes, but...

If the field you change affects the hashCode() of the object bad things will happen. Because if you search for that object later on it will not be in the proper bin and you won't find it.

For example, Jane gets married, but you are also using her name as the hash-key.

map.get("Jane Meyer").setName("Jane Meyer-Jones");  // "legal"

map.get("Jane Meyer") returns the new married version of "Jane Meyer-Jones".
but
map.get("Jane Meyer-Jones") returns null.

3 Comments

p.s. Note that if the change affects equals() then things get even worse, as map.get("Jane Meyer") may not work either.
State changes affecting hashCode() and equals() don't really matter when they're done to values. They matter when they're done to keys in a map.
I appreciate these answers. I dont think these points about affecting the hashCode() of the object are relevant in the context to my application. If I am mistaken, any help would be appreciated. As I am using account numbers(int) and userID(string) as keys and neither of these values are references to fields within the "value" they point to. Neither of these values, userID and account number, are changable within my program. Am i likely to experience problems?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.