Modifying member of object in Map
If i create a class that has a member variable called "testName", and then create a few objects from this and place them all as values in a "Map". How can i iterate through this Map and modify the "testName" variable in each value Object?
In other words how can i access & modify members of an Object when 开发者_运维百科that object has been placed in a Map.
If the objects you want modified are all values in the map, and you don't want to change the mappings from key to value, you can iterate through a collection of just the map's values:
Collection< ValueType > vals = map.values();
for (ValueType val : vals) {
val.testName = ...
}
You have to iterate through each of the entries of the Map and modify the name. See here for example on how to iterate through the Map
A Map is not by itself iterable, but you can get the keySet from the map via the keySet() method and since this is a Set is iterable (implements the Iterable interface). Iterate through the keySet obtaining each value from the Map via its get method and make the changes you desire.
精彩评论