I want to store some objects and then be able to retrieve them later as efficiently as possible. I will also remove some of them under certain conditions. It seems a hash map would be the right choice.
But, from what I’ve seen, hash maps always associate a value with another? For example, “john” and “555-5555”, his phone number.
Now, my situation. Suppose I have a bunch of people, and each person is connected to other people. So, I need each person to store its contacts.
What I’m doing is have each person have a hashmap, and then I’d add to the hash otherPerson, otherPerson. Basically, the key is the value. Am I doing it wrong?
EDIT I don’t think the HashSet would solve my problem because I have to retrieve the value to update it and there is no get method. Remove returns a boolean, so I can’t even remove it to put it back again, which would probably be a bad idea anyway.
This is a puzzling statement. Why would you want to retrieve a value using a
getmethod to update it? Surely, if you know which object you need to retrieve from the set/map, you don’t need to retrieve it.For example:
Now if you are worried that the object that was removed was equal to, but not identical to
p, it seems to me that something is wrong with your design. Either:Personinstances that are equal, orPersoninstances are not identical, orequals(Object).In short, the problem is that you are not managing object identity properly.