HashMap.values() question
Hello,
I have a map say of HashMap(Id,SomeObject);
I populate the above map with some data.
Next I do List l = HashMap.values();
Next I do Object o = HashMap.get(someKey);
o.setSomeThing("Test");
will that automatically update the "value" item in List l? Are the values in List l pointing to the same reference as the values in the HashMap?
Or does the statement on HashMap.values() method about the list being backed by the map and vice versa only pertain to addtion and removals?
[530 byte] By [
robucf3a] at [2007-10-3 4:23:54]

> will that automatically update the "value" item in> List l? yes> Are the values in List l pointing to the> same reference as the values in the HashMap?yes
> will that automatically update the "value" item in List l?
Yes
> Are the values in List l pointing to the same reference as the values in the HashMap?
Yes
> Or does the statement on HashMap.values() method about the list being backed
> by the map and vice versa only pertain to addtion and removals?
This is not the same thing here. The backing is between the map and the collection.
It has nothing to do with the values themselves.
Ok now please explain to me why my code does not work as I expect it to by your replies:
Grid is a concurrent hash map.
private ArrayList<BaseEntity> cache = (ArrayList<BaseEntity>) (grid.values());
BaseEntity entity = (BaseEntity) grid.get(id.getUniqueId());
entity.setLocation(newLoc);
grid.replace(id.getUniqueId(), entity); (I have tried put, combo of remove and add -- I was desperate)
when I get the entity from the cache the value is unchanged. I have no idea what I am doing wrong.
Because you've changed the question.
Your first question was about what happens if you modify an element in the collection. As values() returns the same objects the change is seen whether accessed via the map or via the values() set.
Your second question is about what happens if you replace an object in the map. In that case the values() set you have already obtained is out of date so the replacement object isn't seen.
ejpa at 2007-7-14 22:26:18 >

Please be patient with me I have talked to several coworkers and friends and each have a different answer. So the Collection that is passed back is fixed? So why does the documentation speak of addtions and removals as being "changes"? If you remove something from via the List it is removed from the map, if you add something to the map it should show up in the list.
The thing is I have tried
replace,
remove + put
put
but no matter what I do, when I change the object the change is not reflected in the Collection. So what our saying is when I get the object from the map and modify it, that's all I have to do? the change should be both in the map and the collection?
> Your second question is about what happens if you
> replace an object in the map. In that case the
> values() set you have already obtained is out of date
> so the replacement object isn't seen.
Can you explain what you are talking about? A reference to an object doesn't get "out of date" to my knowledge.
I talked to the developer who extended hashmap, he intentionally changed values() to return copies, so thats why my code seems broken but in actuality my code should have worked given a "normal" hashmap. Sorry for any confusion, I just thought he kept the policy of hashmap when he extended it. Thats a day worth of debugging I can never get back, lol.
Message was edited by:
robucf3
Message was edited by:
robucf3
In that case, he should be fired.
What he has written in NOT a Map. An API consists of both the binary part (i.e. the interface definition) and the semantic part which is usually captured in the documentation.
The documentation for Map.values() specifically states:
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.
If the implementation you have to hand doesn't do this, it's not an implementation of Map.
Out of curiousity, can the guy justify doing what he's done?
I am not sure what the intention of this extension of HashMap is supposed to do, something about distributed maps, etc. I am new to the project so I was just using the API assuming it worked like a hashmap, since it was an extension of a hashmap.
Hence why it's called an API! :-)
> Thats a day worth of debugging I can never get back, lol.Only one day? You're lucky!
Well i could have saved myself some grief if I had just written a simple test program (which I eventually did) to see for myself how a map is supposed to work. Once I did that I saw there was nothing wrong with my code. But I just assumed I was doing something wrong so me and the debugger got to know each other oh too well =/