Core Java (二十四) SortedMap,NavigableMap,SortedSet,NavigableSet接口

释放双眼,带上耳机,听听看~!

SortedMap接口

根据比较器进行排序,是一个有序映射表。

Comparator<? super K> comparator() Returns the comparator used to order the keys in this map, or  null if this map uses the  natural ordering of its keys.
Set<Map.Entry<K,V>> entrySet() Returns a  Set view of the mappings contained in this map.
K firstKey() Returns the first (lowest) key currently in this map.
SortedMap<K,V> headMap(K toKey) Returns a view of the portion of this map whose keys are strictly less than  toKey.
Set<K> keySet() Returns a  Set view of the keys contained in this map.
K lastKey() Returns the last (highest) key currently in this map.
SortedMap<K,V> subMap(K fromKey, K toKey) Returns a view of the portion of this map whose keys range from  fromKey, inclusive, to  toKey, exclusive.
SortedMap<K,V> tailMap(K fromKey) Returns a view of the portion of this map whose keys are greater than or equal to  fromKey.
Collection<V> values() Returns a  Collection view of the values contained in this map.

1
1

NavigableMap接口

此接口实现了SortedMap接口,可以使用SortedMap接口的全部方法。其中,这个接口有三个很重要的方法:

NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) Returns a view of the portion of this map whose keys are greater than (or equal to, if  inclusive is true)  fromKey.

1
1
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) Returns a view of the portion of this map whose keys range from  fromKey to  toKey.

1
1
avigableMap<K,V> headMap(K toKey, boolean inclusive) Returns a view of the portion of this map whose keys are less than (or equal to, if  inclusive is true)  toKey.

1
1

SortedSet接口

根据比较器进行排序,是一个有序集。

下面是这个接口的全部方法:

Comparator<? super E>
comparator() Returns the comparator used to order the elements in this set, or  null if this set uses the  natural ordering of its elements.
E
first() Returns the first (lowest) element currently in this set.
SortedSet<E>
headSet(E toElement) Returns a view of the portion of this set whose elements are strictly less than  toElement.
E
last() Returns the last (highest) element currently in this set.
SortedSet<E>
subSet(E fromElement, E toElement) Returns a view of the portion of this set whose elements range from  fromElement, inclusive, to  toElement, exclusive.
SortedSet<E>
tailSet(E fromElement) Returns a view of the portion of this set whose elements are greater than or equal to  fromElement.

NavigableSet接口

此接口有三个很重要的方法:

SortedSet<E> headSet(E toElement) Returns a view of the portion of this set whose elements are strictly less than  toElement.

1
1
SortedSet<E> subSet(E fromElement, E toElement) Returns a view of the portion of this set whose elements range from  fromElement, inclusive, to  toElement, exclusive.
SortedSet<E> tailSet(E fromElement) Returns a view of the portion of this set whose elements are greater than or equal to  fromElement.

1
1

总结上面的接口的方法,其中SortedSet和SortedMap方法返回大于等于form且小于to的所有元素子集;而NavigableSet和NavigableMap方法返回大于form且小于to的所有元素子集,是否包括form和to应由inclusive参数决定。

测试程序

下面是一个测试程序,分别应用了上面的某些方法。


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
1package com.xujin;
2
3import java.util.Arrays;
4import java.util.Comparator;
5import java.util.List;
6import java.util.NavigableMap;
7import java.util.NavigableSet;
8import java.util.SortedMap;
9import java.util.SortedSet;
10import java.util.TreeMap;
11import java.util.TreeSet;
12
13public class LinkedListTest{
14  public static void main(String...arg){     
15      Employee jim = new Employee(&quot;Jim&quot;);
16      Employee bob = new Employee(&quot;Bob&quot;);
17      Employee gin = new Employee(&quot;Gin&quot;);
18     
19      Employee[] staff = new Employee[3];
20      staff[0] = jim;
21      staff[1] = bob;
22      staff[2] = gin;
23      List&lt;Employee&gt; staffList = Arrays.asList(staff);
24     
25      System.out.println(staffList);//[Name:Jim, Name:Bob, Name:Gin]
26      System.out.println(staffList.get(2));//Name:Gin
27     
28     
29      /************SortedMap接口*******************************************/
30      SortedMap&lt;String, Employee&gt; sm = new TreeMap&lt;String, Employee&gt;();
31      sm.put(&quot;123&quot;, jim);
32      sm.put(&quot;234&quot;, bob);
33      sm.put(&quot;345&quot;, gin);
34      System.out.println(sm);//{123=Name:Jim, 234=Name:Bob, 345=Name:Gin}
35     
36      SortedMap&lt;String, Employee&gt; smSub = sm.subMap(&quot;123&quot;, &quot;345&quot;);//包括123,不包括345
37      System.out.println(smSub);//{123=Name:Jim, 234=Name:Bob}
38      smSub = sm.headMap(&quot;234&quot;);//返回比234小的,不包括234
39      System.out.println(smSub);//{123=Name:Jim}
40      smSub = sm.tailMap(&quot;234&quot;);//返回比234大的,不包括234
41      System.out.println(smSub);//{345=Name:Gin}
42     
43     
44      /************NavigableMap接口*******************************************/
45      NavigableMap&lt;String, Employee&gt; nm = new TreeMap&lt;String, Employee&gt;();
46      nm.put(&quot;123&quot;, jim);
47      nm.put(&quot;234&quot;, bob);
48      nm.put(&quot;345&quot;, gin);
49      System.out.println(nm);//{123=Name:Jim, 234=Name:Bob, 345=Name:Gin}
50     
51      NavigableMap&lt;String, Employee&gt; nmSub = nm.subMap(&quot;123&quot;, true, &quot;345&quot;, false);//包括123,不包括345
52      System.out.println(nmSub);//{123=Name:Jim, 234=Name:Bob}
53      nmSub = nm.headMap(&quot;234&quot;, true);//返回比234小的,true表示包括234
54      System.out.println(nmSub);//{123=Name:Jim, 234=Name:Bob}
55      nmSub = nm.tailMap(&quot;234&quot;, true);//返回比234大的,true表示包括234
56      System.out.println(nmSub);//{234=Name:Bob, 345=Name:Gin}
57     
58     
59      /************SortedSet接口*******************************************/
60      SortedSet&lt;Employee&gt; ss = new TreeSet&lt;Employee&gt;(
61              new Comparator&lt;Employee&gt;(){
62                      public int compare(Employee a, Employee b){
63                          return a.getName().compareTo(b.getName());
64                      }
65              }
66              );
67      ss.add(jim);
68      ss.add(bob);
69      ss.add(gin);
70      System.out.println(ss);//[Name:Bob, Name:Gin, Name:Jim]
71     
72      //以下的特点是,包括from,不包括to
73      SortedSet&lt;Employee&gt; ssSub = ss.subSet(gin, jim);//返回比gin大,比jim小的,不包括jim
74      System.out.println(ssSub);//[Name:Gin]
75      ssSub = ss.headSet(gin);//返回比gin小的,不包括gin
76      System.out.println(ssSub);//[Name:Bob]
77      ssSub = ss.tailSet(gin);//返回比gin大的,包括gin
78      System.out.println(ssSub);//[Name:Gin, Name:Jim]
79     
80     
81      /************NavigableSet接口*******************************************/
82      NavigableSet&lt;Employee&gt; ns = new TreeSet&lt;Employee&gt;(
83              new Comparator&lt;Employee&gt;(){
84                      public int compare(Employee a, Employee b){
85                          return a.getName().compareTo(b.getName());
86                      }
87              }
88              );
89      ns.add(jim);
90      ns.add(bob);
91      ns.add(gin);
92      System.out.println(ns);//[Name:Bob, Name:Gin, Name:Jim]
93     
94      //以下的特点是,根据inclusive参数决定是否包括from,to
95      NavigableSet&lt;Employee&gt; nsSub = ns.subSet(gin, true, jim, true);//返回比gin大,比jim小的,true表示包括gin和jim
96      System.out.println(nsSub);//[Name:Gin, Name:Jim]
97      nsSub = ns.headSet(gin, true);//返回比gin小的,true表示包括gin
98      System.out.println(nsSub);//[Name:Bob, Name:Gin]
99      nsSub = ns.tailSet(gin, true);//返回比gin大的,包括gin
100     System.out.println(nsSub);//[Name:Gin, Name:Jim]
101 }
102}
103
104class Employee{     
105 public Employee(String name){
106     this.name = name;
107 }
108
109 public String getName(){
110     return this.name;
111 }
112
113 public String toString(){
114     return &quot;Name:&quot; + name;
115 }
116
117 private String name = &quot;&quot;;//实例域初始化
118}
119

给TA打赏
共{{data.count}}人
人已打赏
安全技术

Bootstrap框架之排版

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索