-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
59 lines (38 loc) · 1.83 KB
/
Solution.java
File metadata and controls
59 lines (38 loc) · 1.83 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
//And it was commanded them that they should not hurt the grass of the earth, neither any green thing, neither any tree; but only those men which have not the seal of God in their foreheads. (Revelation 9:4)
package com.javarush.task.task35.task3504;
import java.util.HashMap;
import java.util.LinkedHashMap;
/*
Простой generics
*/
public class Solution<T extends HashMap> {
private T map;
public Solution(T map) {
this.map = map;
}
public HashMap getMap() {
return map;
}
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("string", 4);
Solution solution = new Solution(hashMap);
HashMap mapFromSolution = solution.getMap();
System.out.println(mapFromSolution.getClass());
LinkedHashMap<Solution, Solution> hashMap2 = new LinkedHashMap<>();
hashMap2.put(solution, solution);
Solution solution2 = new Solution(hashMap2);
LinkedHashMap mapFromSolution2 = (LinkedHashMap)solution2.getMap(); //need to cast :(
System.out.println(mapFromSolution2.getClass());
}
}
/*
Простой generics
Параметризируй класс Solution, чтобы он мог работать со всеми классами, которые наследуются от HashMap.
Метод getMap должен возвращать тип поля map.
Требования:
1. Класс Solution должен быть параметризирован типом который является наследником HashMap.
2. В классе Solution должно существовать поле map.
3. В классе Solution должен существовать метод getMap.
4. Метод main должен выводить данные на экран.
*/