-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeMapTest.java
More file actions
145 lines (124 loc) · 3.86 KB
/
TreeMapTest.java
File metadata and controls
145 lines (124 loc) · 3.86 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import org.junit.Test;
import java.util.*;
/**
* @ Description:
* @ Author: Jay
* @ Date: Create in 19:56 2021/4/15
* @ Version:
*/
public class TreeMapTest {
//自然排序
@Test
public void TreeMapTest1() {
TreeMap map = new TreeMap();
User u1 = new User("Tom", 23);
User u2 = new User("Tom", 24);
User u3 = new User("Marry", 23);
User u4 = new User("Jack", 23);
User u5 = new User("Rose", 24);
map.put(u1, 98);
map.put(u2, 89);
map.put(u3, 76);
map.put(u4, 100);
map.put(u5, 45);
//遍历
Set entryMap = map.entrySet();
Iterator iterator = entryMap.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
Map.Entry entry = (Map.Entry) obj;
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
}
//定制排序:重写了compare方法后,比较key是否相等时就会调用compare比较
//下面例子中,compare()只涉及age属性,所以插入时年龄相同的对象会被当做key相同
@Test
public void TreeMapTest2() {
TreeMap map = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof User && o2 instanceof User) {
User u1 = (User) o1;
User u2 = (User) o2;
return Integer.compare(u1.getAge(), u2.getAge());
}
throw new RuntimeException("类型不匹配");
}
});
User u1 = new User("Tom", 23);
User u2 = new User("Jerry", 23);
User u3 = new User("Marry", 23);
User u4 = new User("Tom", 26);
User u5 = new User("Rose", 24);
map.put(u1, 98);
map.put(u2, 89);//无法插入,因为年龄相同,compare()返回0
map.put(u3, 76);//修改了u1的value值为76
map.put(u4, 100);//年龄不同,不同key
map.put(u5, 45);
//遍历
Set entryMap = map.entrySet();
Iterator iterator = entryMap.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
Map.Entry entry = (Map.Entry) obj;
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
}
}
class User implements Comparable {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (age != user.age) return false;
return name != null ? name.equals(user.name) : user.name == null;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
//按照姓名从大到小,年龄从小到大
@Override
public int compareTo(Object o) {
if (o instanceof User) {
User user = (User) o;
int compare = -this.name.compareTo(user.name);
if (compare != 0) {
return compare;
} else {
return Integer.compare(this.age, user.age);
}
}
throw new RuntimeException("类型不匹配");
}
}