user1621988
user1621988

Reputation: 4335

How to set the values from one hashmap to another hashmap?

ClassData customClass= customclass.get("John" + 1 );
ClassData currentClass= currentclass.get("John");
currentClass = customClass;

public Map<String, ClassData> currentclass = new HashMap<String, ClassData>();
public Map<String, ClassData> customclass = new HashMap<String, ClassData>();

Is this possible to set the ClassData of customclass to currentclass in this way? Or should I set / get it for each attribute of ClassData? or what other way is really efficient, without I remove any values of a other hashmap key.

Upvotes: 1

Views: 5456

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234795

Your code won't change the maps at all. If you want to set a single entry in one map to the value from another, the easiest thing is to do it explicitly:

currentclass.put("John", customclass.get("John" + 1));

However, if you want to copy all the key/value pairs of one map into another, you can use the putAll method of Map:

currentclass.putAll(customclass);

Upvotes: 3

Related Questions