Hi guys,
and don`t have any problem with the task but logically I don`t understand why I have to clone the users as well?
kind regards
Fabian
package com.codegym.task.task21.task2107;
import java.util.LinkedHashMap;
import java.util.Map;
/*
Deep cloning of a map
*/
public class Solution implements Cloneable {
public static void main(String[] args) {
Solution solution = new Solution();
solution.users.put("Hubert", new User(172, "Hubert"));
solution.users.put("Zapp", new User(41, "Zapp"));
Solution clone = null;
try {
clone = (Solution) solution.clone();
System.out.println(solution);
System.out.println(clone);
System.out.println(solution.users);
System.out.println(clone.users);
} catch (CloneNotSupportedException e) {
e.printStackTrace(System.err);
}
}
@Override
protected Solution clone() throws CloneNotSupportedException {
Solution solution = (Solution) super.clone();
solution.users = new LinkedHashMap<>(this.users);
return solution;
}
protected Map<String, User> users = new LinkedHashMap();
public static class User implements Cloneable {
int age;
String name;
public User(int age, String name) {
this.age = age;
this.name = name;
}
}
}