forked from algorithmzuo/algorithm-primary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode05_HashMapTreeMap.java
More file actions
93 lines (71 loc) · 2.47 KB
/
Code05_HashMapTreeMap.java
File metadata and controls
93 lines (71 loc) · 2.47 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
package class03;
import java.util.HashMap;
import java.util.TreeMap;
public class Code05_HashMapTreeMap {
public static class Node {
public int value;
public Node(int v) {
value = v;
}
}
// (K V)表
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("zuochengyun", "我是左程云");
System.out.println(map.containsKey("zuochengyun"));
System.out.println(map.containsKey("zuo"));
System.out.println(map.get("zuochengyun"));
map.put("zuochengyun", "他是左程云");
System.out.println(map.get("zuochengyun"));
// map.remove("zuochengyun");
// System.out.println(map.containsKey("zuochengyun"));
// System.out.println(map.get("zuochengyun"));
String test1 = "zuochengyun";
String test2 = "zuochengyun";
System.out.println(map.containsKey(test1));
System.out.println(map.containsKey(test2));
HashMap<Integer, String> map2 = new HashMap<>();
map2.put(1234567, "我是1234567");
Integer a = 1234567;
Integer b = 1234567;
System.out.println(a == b);
System.out.println(map2.containsKey(a));
System.out.println(map2.containsKey(b));
Node node1 = new Node(1);
Node node2 = new Node(1);
HashMap<Node, String> map3 = new HashMap<>();
map3.put(node1, "我进来了!");
System.out.println(map3.containsKey(node1));
System.out.println(map3.containsKey(node2));
System.out.println("===================");
TreeMap<Integer, String> treeMap1 = new TreeMap<>();
treeMap1.put(3, "我是3");
treeMap1.put(0, "我是3");
treeMap1.put(7, "我是3");
treeMap1.put(2, "我是3");
treeMap1.put(5, "我是3");
treeMap1.put(9, "我是3");
System.out.println(treeMap1.containsKey(7));
System.out.println(treeMap1.containsKey(6));
System.out.println(treeMap1.get(3));
treeMap1.put(3, "他是3");
System.out.println(treeMap1.get(3));
treeMap1.remove(3);
System.out.println(treeMap1.get(3));
System.out.println(treeMap1.firstKey());
System.out.println(treeMap1.lastKey());
// <=5 离5最近的key告诉我
System.out.println(treeMap1.floorKey(5));
// <=6 离6最近的key告诉我
System.out.println(treeMap1.floorKey(6));
// >=5 离5最近的key告诉我
System.out.println(treeMap1.ceilingKey(5));
// >=6 离6最近的key告诉我
System.out.println(treeMap1.ceilingKey(6));
// Node node3 = new Node(3);
// Node node4 = new Node(4);
// TreeMap<Node, String> treeMap2 = new TreeMap<>();
// treeMap2.put(node3, "我是node3");
// treeMap2.put(node4, "我是node4");
}
}