HashMap<String, Object> How to replace 1 value of the Object?
It depends on whether Data
is mutable. For example, you may be able to write:
Data data = map.get("jan");
data.setColor("Blue");
Don't forget that the map only contains a reference to 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 withColor
method, so you could write:
Data data = map.get("jan");
map.put("jan", data.withColor("Blue"));
Without knowing more about your Data
type (which I hope isn't the real name of your class) it's hard to say any more.
(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...)
Assuming Data
is mutable you can set the "RED"
field:
Map map = new HashMap();
map.put("jan", new Data("RED","M4A1",5,0,0));
// Later...
map.get("jan").setColor("BLUE");
If Data
isn't mutable, then your only option is to put
the new value as you have it written.