Skip to content

Commit b476a08

Browse files
committed
Added a new tasks to level 21 (lesson 08 - task 01).
1 parent 8997b39 commit b476a08

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.javarush.test.level21.lesson08.task01;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
/* Глубокое клонирование карты
7+
Клонируйтие объект класса Solution используя глубокое клонирование.
8+
Данные в карте users также должны клонироваться.
9+
*/
10+
public class Solution
11+
{
12+
public static void main(String[] args)
13+
{
14+
Solution solution = new Solution();
15+
solution.users.put("Hubert", new User(172, "Hubert"));
16+
solution.users.put("Zapp", new User(41, "Zapp"));
17+
try
18+
{
19+
Solution clone = solution.clone();
20+
System.out.println(solution);
21+
System.out.println(clone);
22+
23+
System.out.println(solution.users);
24+
System.out.println(clone.users);
25+
26+
}
27+
catch (CloneNotSupportedException e)
28+
{
29+
e.printStackTrace(System.err);
30+
}
31+
}
32+
33+
protected Map<String, User> users = new LinkedHashMap();
34+
35+
public static class User
36+
{
37+
int age;
38+
String name;
39+
40+
public User(int age, String name)
41+
{
42+
this.age = age;
43+
this.name = name;
44+
}
45+
46+
@Override
47+
protected User clone() throws CloneNotSupportedException
48+
{
49+
return new User(this.age, this.name);
50+
}
51+
}
52+
53+
@Override
54+
protected Solution clone() throws CloneNotSupportedException
55+
{
56+
Map<String, User> users = new LinkedHashMap();
57+
for (Map.Entry<String, User> entry : this.users.entrySet())
58+
{
59+
User user = entry.getValue().clone();
60+
users.put(entry.getKey(), user);
61+
}
62+
Solution solution = new Solution();
63+
solution.users.putAll(users);
64+
return solution;
65+
}
66+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy