java HashMap<String, Object> 如何替换对象的 1 个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12516682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:12:20  来源:igfitidea点击:

HashMap<String, Object> How to replace 1 value of the Object?

javaobjectmaphashmap

提问by user1621988

Map<String, Data> map = new HashMap<String,Data>();
map.put("jan", new Data("RED","M4A1",5,0,0));

How can I change the value RED of the Data object?, without getting all the information out of the map with the key and putting it back in, like this:

如何更改 Data 对象的值 RED?,而无需使用键从地图中取出所有信息并将其放回原处,如下所示:

map.put("jan" new Data("Blue",
      map.get("jan").Brand,
      map.get("jan").Storage,
      map.get("jan").Sold,
      map.get("jan").Bought)); 

So how can i change 1 value of the Data Object instead of redo them all?

那么如何更改数据对象的 1 个值而不是全部重做?

回答by Jon Skeet

It depends on whether Datais mutable. For example, you may be able to write:

这取决于是否Data可变。例如,您可以编写:

Data data = map.get("jan");
data.setColor("Blue");

Don't forget that the map only contains a referenceto the object, so if you change the data within the object, that change will be seen if someone fetches the reference from the map later.

不要忘记地图只包含对对象的引用,因此如果您更改对象内的数据,那么如果稍后有人从地图中获取引用,则会看到该更改。

Or if it's immutable, it could potentially have a withColormethod, so you could write:

或者如果它是不可变的,它可能有一个withColor方法,所以你可以写:

Data data = map.get("jan");
map.put("jan", data.withColor("Blue"));

Without knowing more about your Datatype (which I hope isn't the real name of your class) it's hard to say any more.

如果不了解您的Data类型(我希望这不是您班级的真实姓名),就很难再多说了。

(I also hope your class doesn't really have Pascal-cased fields, and I hope those fields are private, but that's a different matter...)

(我也希望你的班级没有真正的 Pascal-cased 字段,我希望这些字段是私有的,但那是另一回事......)

回答by Brian

Assuming Datais mutable you can set the "RED"field:

假设Data是可变的,您可以设置该"RED"字段:

Map map = new HashMap();
map.put("jan", new Data("RED","M4A1",5,0,0));
// Later...
map.get("jan").setColor("BLUE");

If Dataisn't mutable, then your only option is to putthe new value as you have it written.

如果Data不是可变的,那么您唯一的选择就是使用put您编写的新值。

回答by Jo?o Silva

Assuming Datahas a setterfor the colorproperty:

假设Data有一settercolor属性:

public class Data {
  private String color;

  public void setColor(String color) {
    this.color = color;
  }
}

You can just getthe required Dataobject and set its property:

您可以只get需要所需的Data对象并设置其属性:

Data data = map.get("jan");
data.setColor("blue");

回答by AlexR

Add appropriate setter to your Dataclass, e.g.

将适当的 setter 添加到您的Data类中,例如

class Data {
    setColor(String color){...}
}


map.get("jan").setColor("BLUE");

回答by Shreyos Adikari

Please find the code snippet:

请找到代码片段:

Map<String, Data> map = new HashMap<String,Data>();

public class Data{

private String color;

  public void setColor(String color) {
    this.color = color;
  }

public String getColor() {
    return this.color;
  }

publicData(String color){
this.setColor(color);
}
}


map.put("jan", new Data("RED","M4A1",5,0,0));

Now you may do like this:

现在你可以这样做:

Data data = map.get("jan");
data.setColor("black");

This will work.

这将起作用。

Here the class Data contains only one field i.e. color. You can add more fields. Thanks

这里的类 Data 只包含一个字段,即颜色。您可以添加更多字段。谢谢