forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ2Lesson3.java
More file actions
98 lines (91 loc) · 2.91 KB
/
J2Lesson3.java
File metadata and controls
98 lines (91 loc) · 2.91 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
/**
* Java. Level 2. Lesson 3. Collection Framework
* Examples for lesson
*
* @author Sergey Iryupin
* @version dated Jun 11, 2018
* @see also Wrapper/Decorator (Integer, Long, Float, Double...)
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
class J2Lesson3 {
public static void main(String[] args) {
ArrayListExample();
HashSetExample();
TreeSetExample();
HashMapExample();
TreeMapExample();
}
// @see https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
static void ArrayListExample() {
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
System.out.println(list);
list.add(1, "A1");
System.out.println(list);
list.remove("E");
System.out.println("We'll remove " + list.get(2));
list.remove(2);
System.out.println(list);
}
// @see https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html
static void HashSetExample() {
Set<String> hs = new HashSet<>();
hs.add("Beta");
hs.add("Alpha");
hs.add("Eta");
hs.add("Gamma");
hs.add("Epsilon");
hs.add("Omega");
hs.add("Gamma");
System.out.println(hs);
}
// @see https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html
static void TreeSetExample() {
Set<String> hs = new TreeSet<>();
hs.add("Beta");
hs.add("Alpha");
hs.add("Eta");
hs.add("Gamma");
hs.add("Epsilon");
hs.add("Omega");
hs.add("Gamma");
System.out.println(hs);
}
// @see https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
static void HashMapExample() {
Map<String, String> hm = new HashMap<>();
hm.put("Moscow", "Russia");
hm.put("Rostov", "Russia");
hm.put("Paris", "France");
hm.put("Berlin", "Germany");
hm.put("Oslo", "Norway");
Set<Map.Entry<String, String>> set = hm.entrySet();
for (Map.Entry<String, String> o : set)
System.out.println(o.getKey() + ": " + o.getValue());
System.out.println(hm);
System.out.println(hm.get("Paris"));
}
// @see https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
static void TreeMapExample() {
Map<String, String> tm = new TreeMap<>();
tm.put("Moscow", "Russia");
tm.put("Paris", "France");
tm.put("Berlin", "Germany");
tm.put("Oslo", "Norway");
Set<Map.Entry<String, String>> set = tm.entrySet();
for(Map.Entry<String, String> o : set)
System.out.println(o.getKey() + ": " + o.getValue());
System.out.println(tm);
}
}