SIGSTP
SIGSTP

Reputation: 1155

Copying content of one class to another

I have a map:

Map<String, List<ClassA>> X

Where class A is serializable.

and another map

Map<String, List<ClassB>> Y

Where class B is not serializable.

Content of both Class A and Class B are same.

Now I want to copy map X to map Y but I cant do it directly by assigning because Classes are different.

So I thought to extend Class B to Class A by just adding extends Class B in Class A.

But then also I am not able to copy the Map X to Map Y since parent can not be assigned to child so I thought of casting but since it map I am not able to think of casting.

Any suggestions that how can I make both class compatible?

P.S.: Here I don't want to extend since extending is not a proper method.

Upvotes: 0

Views: 86

Answers (1)

Adam Siemion
Adam Siemion

Reputation: 16029

You can use method putAll() from java.util.Map, Javadoc.

void putAll(Map<? extends K, ? extends V> m);

Copies all of the mappings from the specified map to this map (optional operation).

In your scenario:

Y.putAll(X);

Upvotes: 1

Related Questions