[英]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". 如果我创建一个类,该类具有一个名为“ testName”的成员变量,然后从中创建一些对象,并将它们全部作为值放置在“ Map”中。 How can i iterate through this Map and modify the "testName" variable in each value Object?
我如何遍历此Map并修改每个值Object中的“ testName”变量?
In other words how can i access & modify members of an Object when that object has been placed in a Map. 换句话说,将对象放置在地图中后,我该如何访问和修改该对象的成员。
You have to iterate through each of the entries of the Map and modify the name. 您必须遍历 Map的每个条目并修改名称。 See here for example on how to iterate through the 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 = ...
}
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). Map本身不是可迭代的,但是您可以通过keySet()方法从映射中获取keySet,并且由于这是一个Set是可迭代的(实现Iterable接口)。 Iterate through the keySet obtaining each value from the Map via its get method and make the changes you desire.
遍历keySet并通过其get方法从Map中获取每个值,然后进行所需的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.