类关系图

SortedMap - 图1

功能介绍

包路径:java.util

说明:SortedMap直接继承了Map接口,提供了若干个与排序相关的方法,如获取最大最小key值等

SortedMap典型实现子类常用的几个如NavigableMap接口、TreeMap类(实现NavigableMap)等。

源码解析

SortedMap

  1. /**
  2. *
  3. * 直接继承Map接口,提供了若干个与排序相关的方法,使其能够进行排序
  4. *
  5. * @param <K> the type of keys maintained by this map
  6. * @param <V> the type of mapped values
  7. *
  8. * @author Josh Bloch
  9. * @see Map
  10. * @see TreeMap
  11. * @see SortedSet
  12. * @see Comparator
  13. * @see Comparable
  14. * @see Collection
  15. * @see ClassCastException
  16. * @since 1.2
  17. */
  18. public interface SortedMap<K,V> extends Map<K,V> {
  19. /**
  20. * Returns the comparator used to order the keys in this map, or
  21. * {@code null} if this map uses the {@linkplain Comparable
  22. * natural ordering} of its keys.
  23. *
  24. * 返回一个比较器,内部使用key的自然大小排序
  25. *
  26. * @return the comparator used to order the keys in this map,
  27. * or {@code null} if this map uses the natural ordering
  28. * of its keys
  29. */
  30. Comparator<? super K> comparator();
  31. /**
  32. * Returns a view of the portion of this map whose keys range from
  33. * {@code fromKey}, inclusive, to {@code toKey}, exclusive. (If
  34. * {@code fromKey} and {@code toKey} are equal, the returned map
  35. * is empty.) The returned map is backed by this map, so changes
  36. * in the returned map are reflected in this map, and vice-versa.
  37. * The returned map supports all optional map operations that this
  38. * map supports.
  39. *
  40. * 截取Map,返回从fromKey到toKey的映射子集合【左开右闭】
  41. *
  42. * @param fromKey low endpoint (inclusive) of the keys in the returned map
  43. * @param toKey high endpoint (exclusive) of the keys in the returned map
  44. * @return a view of the portion of this map whose keys range from
  45. * {@code fromKey}, inclusive, to {@code toKey}, exclusive
  46. */
  47. SortedMap<K,V> subMap(K fromKey, K toKey);
  48. /**
  49. * Returns a view of the portion of this map whose keys are
  50. * strictly less than {@code toKey}. The returned map is backed
  51. * by this map, so changes in the returned map are reflected in
  52. * this map, and vice-versa. The returned map supports all
  53. * optional map operations that this map supports.
  54. *
  55. * 返回key值小于toKey的Map映射
  56. *
  57. * @param toKey high endpoint (exclusive) of the keys in the returned map
  58. * @return a view of the portion of this map whose keys are strictly
  59. * less than {@code toKey}
  60. */
  61. SortedMap<K,V> headMap(K toKey);
  62. /**
  63. * Returns a view of the portion of this map whose keys are
  64. * greater than or equal to {@code fromKey}. The returned map is
  65. * backed by this map, so changes in the returned map are
  66. * reflected in this map, and vice-versa. The returned map
  67. * supports all optional map operations that this map supports.
  68. *
  69. * 返回key值大于fromKey的Map映射
  70. *
  71. * @param fromKey low endpoint (inclusive) of the keys in the returned map
  72. * @return a view of the portion of this map whose keys are greater
  73. * than or equal to {@code fromKey}
  74. */
  75. SortedMap<K,V> tailMap(K fromKey);
  76. /**
  77. * Returns the first (lowest) key currently in this map.
  78. *
  79. * 返回最小key
  80. *
  81. * @return the first (lowest) key currently in this map
  82. * @throws NoSuchElementException if this map is empty
  83. */
  84. K firstKey();
  85. /**
  86. * Returns the last (highest) key currently in this map.
  87. *
  88. * 返回最大key
  89. *
  90. * @return the last (highest) key currently in this map
  91. * @throws NoSuchElementException if this map is empty
  92. */
  93. K lastKey();
  94. /**
  95. *返回key的set集合
  96. *
  97. * @return a set view of the keys contained in this map, sorted in
  98. * ascending order
  99. */
  100. Set<K> keySet();
  101. /**
  102. * 返回value的Collection集合
  103. *
  104. * @return a collection view of the values contained in this map,
  105. * sorted in ascending key order
  106. */
  107. Collection<V> values();
  108. /**
  109. * 返回Entry映射集合
  110. *
  111. * @return a set view of the mappings contained in this map,
  112. * sorted in ascending key order
  113. */
  114. Set<Map.Entry<K, V>> entrySet();
  115. }

NavigableMap

SortedMap接口主要提供了获取最大值与最小值的方法,但对于一个已经排序的数据集,除了最大值与最小值之外,我们还需要些别的功能,更快的,比如获取比某个key值大或者小的值等

NavigableMap继承了SortedMap接口,主要扩展了一些便于匹配的导航方法,且主要是为了查找而不是遍历

  1. /**
  2. * @author Doug Lea
  3. * @author Josh Bloch
  4. * @param <K> the type of keys maintained by this map
  5. * @param <V> the type of mapped values
  6. * @since 1.6
  7. */
  8. public interface NavigableMap<K,V> extends SortedMap<K,V> {
  9. /**
  10. * 获取比传入key值小的最大映射
  11. *
  12. * @param key the key
  13. * @return an entry with the greatest key less than {@code key},
  14. * or {@code null} if there is no such key
  15. */
  16. Map.Entry<K,V> lowerEntry(K key);
  17. /**
  18. * 获取比传入key值小的最大key
  19. *
  20. * @param key the key
  21. * @return the greatest key less than {@code key},
  22. * or {@code null} if there is no such key
  23. *
  24. */
  25. K lowerKey(K key);
  26. /**
  27. * 返回键值小于等于传入key的最大映射
  28. *
  29. * @param key the key
  30. * @return an entry with the greatest key less than or equal to
  31. * {@code key}, or {@code null} if there is no such key
  32. *
  33. */
  34. Map.Entry<K,V> floorEntry(K key);
  35. /**
  36. * 返回键值小于等于传入key的最大key值
  37. *
  38. * @param key the key
  39. * @return the greatest key less than or equal to {@code key},
  40. * or {@code null} if there is no such key
  41. *
  42. */
  43. K floorKey(K key);
  44. /**
  45. * 返回大于等于传入key值的第一个映射
  46. *
  47. * @param key the key
  48. * @return an entry with the least key greater than or equal to
  49. * {@code key}, or {@code null} if there is no such key
  50. *
  51. */
  52. Map.Entry<K,V> ceilingEntry(K key);
  53. /**
  54. * 返回大于等于传入key的第一个key值
  55. *
  56. * @param key the key
  57. * @return the least key greater than or equal to {@code key},
  58. * or {@code null} if there is no such key
  59. *
  60. */
  61. K ceilingKey(K key);
  62. /**
  63. * 返回大于传入key的最小映射
  64. *
  65. * @param key the key
  66. * @return an entry with the least key greater than {@code key},
  67. * or {@code null} if there is no such key
  68. *
  69. */
  70. Map.Entry<K,V> higherEntry(K key);
  71. /**
  72. * 返回大于传入key的最小key值
  73. *
  74. * @param key the key
  75. * @return the least key greater than {@code key},
  76. * or {@code null} if there is no such key
  77. *
  78. */
  79. K higherKey(K key);
  80. /**
  81. * 获取最小key映射
  82. *
  83. * @return an entry with the least key,
  84. * or {@code null} if this map is empty
  85. */
  86. Map.Entry<K,V> firstEntry();
  87. /**
  88. * 获取最大key映射
  89. *
  90. * @return an entry with the greatest key,
  91. * or {@code null} if this map is empty
  92. */
  93. Map.Entry<K,V> lastEntry();
  94. /**
  95. * 删除最小key映射
  96. *
  97. * @return the removed first entry of this map,
  98. * or {@code null} if this map is empty
  99. */
  100. Map.Entry<K,V> pollFirstEntry();
  101. /**
  102. * 删除最大key映射
  103. *
  104. * @return the removed last entry of this map,
  105. * or {@code null} if this map is empty
  106. */
  107. Map.Entry<K,V> pollLastEntry();
  108. /**
  109. * 返回倒序Map
  110. *
  111. * @return a reverse order view of this map
  112. */
  113. NavigableMap<K,V> descendingMap();
  114. /**
  115. * 返回一个key的NavigableSet集合,NavigableSet和NavigableMap类似
  116. *
  117. * @return a navigable set view of the keys in this map
  118. */
  119. NavigableSet<K> navigableKeySet();
  120. /**
  121. * 对上述集合倒序
  122. *
  123. * @return a reverse order navigable set view of the keys in this map
  124. */
  125. NavigableSet<K> descendingKeySet();
  126. /**
  127. * 截取Map,返回一个从 fromKey 至 toKey 的子Map,是否包含fromKey和toKey由fromInclusive和toInclusive决定
  128. *
  129. * @param fromKey low endpoint of the keys in the returned map
  130. * @param fromInclusive {@code true} if the low endpoint
  131. * is to be included in the returned view
  132. * @param toKey high endpoint of the keys in the returned map
  133. * @param toInclusive {@code true} if the high endpoint
  134. * is to be included in the returned view
  135. * @return a view of the portion of this map whose keys range from
  136. * {@code fromKey} to {@code toKey}
  137. *
  138. */
  139. NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
  140. K toKey, boolean toInclusive);
  141. /**
  142. * 返回一个键值小于等于fromKey的子Map(inclusive决定是否包含有fromKey)
  143. *
  144. * @param toKey high endpoint of the keys in the returned map
  145. * @param inclusive {@code true} if the high endpoint
  146. * is to be included in the returned view
  147. * @return a view of the portion of this map whose keys are less than
  148. * (or equal to, if {@code inclusive} is true) {@code toKey}
  149. *
  150. */
  151. NavigableMap<K,V> headMap(K toKey, boolean inclusive);
  152. /**
  153. * 返回一个键值大于等于toKey的子Map(inclusive决定是否包含有toKey)
  154. *
  155. * <p>The returned map will throw an {@code IllegalArgumentException}
  156. * on an attempt to insert a key outside its range.
  157. *
  158. * @param fromKey low endpoint of the keys in the returned map
  159. * @param inclusive {@code true} if the low endpoint
  160. * is to be included in the returned view
  161. * @return a view of the portion of this map whose keys are greater than
  162. * (or equal to, if {@code inclusive} is true) {@code fromKey}
  163. *
  164. */
  165. NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
  166. /**
  167. * 返回一个键值从fromKey到toKey的子Map(左开右闭)
  168. *
  169. * <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}.
  170. *
  171. * @throws ClassCastException {@inheritDoc}
  172. * @throws NullPointerException {@inheritDoc}
  173. * @throws IllegalArgumentException {@inheritDoc}
  174. */
  175. SortedMap<K,V> subMap(K fromKey, K toKey);
  176. /**
  177. * 返回一个键值小于toKey的子Map
  178. *
  179. * <p>Equivalent to {@code headMap(toKey, false)}.
  180. */
  181. SortedMap<K,V> headMap(K toKey);
  182. /**
  183. * 返回一个键值大于等于fromKey的子Map
  184. *
  185. * <p>Equivalent to {@code tailMap(fromKey, true)}.
  186. *
  187. */
  188. SortedMap<K,V> tailMap(K fromKey);
  189. }

简单使用

TreeMap实现了NavigableMap,也就相当于实现了SortedMap,我们看一下TreeMap的简单使用

public class TreeMapCode {

    public static void main(String[] args) {
        Map<Integer,String> treeMap = new TreeMap();
        treeMap.put(1, "呵呵呵呵");
        treeMap.put(4, "呸呸呸呸");
        treeMap.put(2, "噢噢噢噢");
        treeMap.put(3, "哈哈哈哈");

        System.out.println("Map = "+treeMap);

        // 获取第一个元素
        Map.Entry<Integer, String> firstEntry = ((TreeMap<Integer, String>) treeMap).firstEntry();

        System.out.println("\n第一个元素 = "+firstEntry);

        // 获取最后一个元素
        Map.Entry<Integer, String> lastEntry = ((TreeMap<Integer, String>) treeMap).lastEntry();
        System.out.println("\n最后一个元素 = "+lastEntry);

        // 截取Map
        SortedMap<Integer, String> subMap = ((TreeMap<Integer, String>) treeMap).subMap(2, 4);
        System.out.println("\n截取2-4之间的子Map = "+subMap);

        // 获取key值小于3的最大映射
        Map.Entry<Integer, String> lowerEntry = ((TreeMap<Integer, String>) treeMap).lowerEntry(3);
        System.out.println("\nkey小于3的最大映射 = "+lowerEntry);

        // 获取key值大于2的最小映射
        Map.Entry<Integer, String> higherEntry = ((TreeMap<Integer, String>) treeMap).higherEntry(2);
        System.out.println("\nkey大于2的最小映射 = "+higherEntry);

        // 倒序排列后返回
        NavigableMap<Integer, String> descendingMap = ((TreeMap<Integer, String>) treeMap).descendingMap();
        System.out.println("\n倒序排列后的Map = "+descendingMap);

    }
}

执行结果:
image.png

总结

1、SortedMap直接继承了Map接口,提供了若干个与排序相关的方法,如获取最大最小key值等

2、NavigableMap继承了SortedMap接口,主要扩展了一些便于匹配的导航方法,且主要是为了查找而不是遍历

3、TreeMap实现了NavigableMap,也就相当于实现了SortedMap,是比较常用的有序Map实现。