在Map集合框架中,除了HashMap以外,TreeMap也是常用到的集合對象之一。
與HashMap相比,TreeMap是一個能比較元素大小的Map集合,會對傳入的key進行了大小排序。其中,可以使用元素的自然順序,也可以使用集合中自定義的比較器來進行排序;
不同於HashMap的哈希映射,TreeMap實現了紅黑樹的結構,形成了一顆二叉樹。
TreeMap繼承於AbstractMap,實現了Map, Cloneable, NavigableMap, Serializable接口。
(1)TreeMap 繼承於AbstractMap,而AbstractMap實現了Map接口,並實現了Map接口中定義的方法,減少了其子類繼承的復雜度;
(2)TreeMap 實現了Map接口,成為Map框架中的一員,可以包含着key-value形式的元素;
(3)TreeMap 實現了NavigableMap接口,意味着擁有了更強的元素搜索能力;
(4)TreeMap 實現了Cloneable接口,實現了clone()方法,可以被克隆;
(5)TreeMap 實現了Java.io.Serializable接口,支持序列化操作;
TreeMap具有如下特點:
不允許出現重復的key;
可以插入null鍵,null值;
可以對元素進行排序;
無序集合(插入和遍歷順序不一致);
TreeMap基本操作
01
02
03
04
05
06
07
08
09
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
|
public
class
TreeMapTest {
public
static
void
main(String[] agrs){
//創建TreeMap對象:
TreeMap<String,Integer> treeMap =
new
TreeMap<String,Integer>();
System.out.println(
"初始化后,TreeMap元素個數為:"
+ treeMap.size());
//新增元素:
treeMap.put(
"hello"
,
1
);
treeMap.put(
"world"
,
2
);
treeMap.put(
"my"
,
3
);
treeMap.put(
"name"
,
4
);
treeMap.put(
"is"
,
5
);
treeMap.put(
"huangqiuping"
,
6
);
treeMap.put(
"i"
,
6
);
treeMap.put(
"am"
,
6
);
treeMap.put(
"a"
,
6
);
treeMap.put(
"developer"
,
6
);
System.out.println(
"添加元素后,TreeMap元素個數為:"
+ treeMap.size());
//遍歷元素:
Set<Map.Entry<String,Integer>> entrySet = treeMap.entrySet();
for
(Map.Entry<String,Integer> entry : entrySet){
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(
"TreeMap元素的key:"
+key+
",value:"
+value);
}
//獲取所有的key:
Set<String> keySet = treeMap.keySet();
for
(String strKey:keySet){
System.out.println(
"TreeMap集合中的key:"
+strKey);
}
//獲取所有的value:
Collection<Integer> valueList = treeMap.values();
for
(Integer intValue:valueList){
System.out.println(
"TreeMap集合中的value:"
+ intValue);
}
//獲取元素:
//獲取集合內元素key為"huangqiuping"的值
Integer getValue = treeMap.get(
"huangqiuping"
);
//獲取集合內第一個元素
String firstKey = treeMap.firstKey();
//獲取集合內最后一個元素
String lastKey =treeMap.lastKey();
//獲取集合內的key小於"huangqiuping"的key
String lowerKey =treeMap.lowerKey(
"huangqiuping"
);
//獲取集合內的key大於等於"huangqiuping"的key
String ceilingKey =treeMap.ceilingKey(
"huangqiuping"
);
//獲取集合的key從"a"到"huangqiuping"的元素
SortedMap<String,Integer> sortedMap =treeMap.subMap(
"a"
,
"my"
);
//刪除元素:
//刪除集合中key為"huangqiuping"的元素
Integer removeValue = treeMap.remove(
"huangqiuping"
);
//清空集合元素:
treeMap.clear();
//判斷方法:
//判斷集合是否為空
boolean
isEmpty = treeMap.isEmpty();
//判斷集合的key中是否包含"huangqiuping"
boolean
isContain = treeMap.containsKey(
"huangqiuping"
);
}
}
|
TreeMap排序
(1)使用元素自然排序
在使用自然順序排序時候,需要區分兩種情況:一種是Jdk定義的對象,一種是自己定義的對象;
01
02
03
04
05
06
07
08
09
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
|
<font style=
"color:rgb(77, 77, 77)"
><font face=
"""
><font style=
"font-size:16px"
>
public
class
SortedTest
implements
Comparable<SortedTest> {
private
int
age;
public
SortedTest(
int
age){
this
.age = age;
}
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
this
.age = age;
}
//自定義對象,實現compareTo(T o)方法:
public
int
compareTo(SortedTest sortedTest) {
int
num =
this
.age - sortedTest.getAge();
//為0時候,兩者相同:
if
(num==
0
){
return
0
;
//大於0時,傳入的參數小:
}
else
if
(num>
0
){
return
1
;
//小於0時,傳入的參數大:
}
else
{
return
-
1
;
}
}
}
public
class
TreeMapTest {
public
static
void
main(String[] agrs){
//自然順序比較
naturalSort();
}
//自然排序順序:
public
static
void
naturalSort(){
//第一種情況:Integer對象
TreeMap<Integer,String> treeMapFirst =
new
TreeMap<Integer, String>();
treeMapFirst.put(
1
,
"huangqiuping"
);
treeMapFirst.put(
6
,
"huangqiuping"
);
treeMapFirst.put(
3
,
"huangqiuping"
);
treeMapFirst.put(
10
,
"huangqiuping"
);
treeMapFirst.put(
7
,
"huangqiuping"
);
treeMapFirst.put(
13
,
"huangqiuping"
);
System.out.println(treeMapFirst.toString());
//第二種情況:SortedTest對象
TreeMap<SortedTest,String> treeMapSecond =
new
TreeMap<SortedTest, String>();
treeMapSecond.put(
new
SortedTest(
10
),
"huangqiuping"
);
treeMapSecond.put(
new
SortedTest(
1
),
"huangqiuping"
);
treeMapSecond.put(
new
SortedTest(
13
),
"huangqiuping"
);
treeMapSecond.put(
new
SortedTest(
4
),
"huangqiuping"
);
treeMapSecond.put(
new
SortedTest(
0
),
"huangqiuping"
);
treeMapSecond.put(
new
SortedTest(
9
),
"huangqiuping"
);
System.out.println(treeMapSecond.toString());
}
}</font></font></font>
|
在自然順序比較中,需要讓被比較的元素實現Comparable接口,否則在向集合里添加元素時報:"java.lang.ClassCastException: com.huangqiuping.collection.map.SortedTest cannot be cast to java.lang.Comparable"異常;
這是因為在調用put()方法時,會將傳入的元素轉化成Comparable類型對象,所以當你傳入的元素沒有實現Comparable接口時,就無法轉換,遍會報錯;
(2)使用自定義比較器排序
使用自定義比較器排序,需要在創建TreeMap對象時,將自定義比較器對象傳入到TreeMap構造方法中;
自定義比較器對象,需要實現Comparator接口,並實現比較方法compare(To1,To2);
使用自定義比較器排序的話,被比較的對象無需再實現Comparable接口了;
01
02
03
04
05
06
07
08
09
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
|
public
class
SortedTest {
private
int
age;
public
SortedTest(
int
age){
this
.age = age;
}
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
this
.age = age;
}
}
public
class
SortedTestComparator
implements
Comparator<SortedTest> {
//自定義比較器:實現compare(To1,To2)方法:
public
int
compare(SortedTest sortedTest1, SortedTest sortedTest2) {
int
num = sortedTest1.getAge() - sortedTest2.getAge();
if
(num==
0
){
//為0時候,兩者相同:
return
0
;
}
else
if
(num>
0
){
//大於0時,后面的參數小:
return
1
;
}
else
{
//小於0時,前面的參數小:
return
-
1
;
}
}
}
|