alius
alius

Reputation: 25

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.

Upvotes: 0

Views: 950

Answers (3)

robert_x44
robert_x44

Reputation: 9314

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 = ...
}

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

Upvotes: 1

Aravind Yarram
Aravind Yarram

Reputation: 80176

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

Upvotes: 2

Related Questions