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:
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?
It depends on whether
Datais mutable. For example, you may be able to write: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
withColormethod, so you could write:Without knowing more about your
Datatype (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…)