Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
128 lines (103 loc) · 3.92 KB
/
Copy pathSolution.java
File metadata and controls
128 lines (103 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Greet Herodion, my kinsman. Greet them of the household of Narcissus, who are in the Lord. (Romans 16:11)
package com.javarush.task.task21.task2107;
import java.util.LinkedHashMap;
import java.util.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.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);
}
}
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;
}
@Override
protected User clone() throws CloneNotSupportedException {
return new User(this.age, this.name);
}
@Override
public int hashCode() {
int res = age;
res = 31*res + name.hashCode();
return res;
}
@Override
public boolean equals(Object o) {
if (this == o) {return true;}
if (!(o instanceof User)) {return false;}
User u = (User)o;
if (age != u.age) {return false;}
return name.equals(u.name);
}
}
@Override
protected Solution clone() throws CloneNotSupportedException {
Solution solution = new Solution();
for (Map.Entry<String, User> pair : this.users.entrySet()) {
User u = pair.getValue().clone();
solution.users.put(pair.getKey(), u);
}
return solution;
}
}
/*
Глубокое клонирование карты
Обеспечь возможность клонирования объекта класса Solution используя глубокое клонирование.
Данные в карте users также должны быть клонированы.
Не забудь о методах equals и hashCode для корректного добавления элементов типа User в HashMap.
Требования:
1. Класс Solution должен поддерживать интерфейс Cloneable.
2. Класс User должен поддерживать интерфейс Cloneable.
3. В классе User должен быть корректно реализован метод clone.
4. В классе Solution должен быть корректно реализован метод clone.
package com.javarush.task.task21.task2107;
import java.util.LinkedHashMap;
import java.util.Map;
*
Глубокое клонирование карты
*
public class Solution {
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.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);
}
}
protected Map<String, User> users = new LinkedHashMap();
public static class User {
int age;
String name;
public User(int age, String name) {
this.age = age;
this.name = name;
}
}
}
*/