类关系图
功能介绍
包路径:java.util
说明:SortedMap直接继承了Map接口,提供了若干个与排序相关的方法,如获取最大最小key值等
SortedMap典型实现子类常用的几个如NavigableMap接口、TreeMap类(实现NavigableMap)等。
源码解析
SortedMap
/**
*
* 直接继承Map接口,提供了若干个与排序相关的方法,使其能够进行排序
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
*
* @author Josh Bloch
* @see Map
* @see TreeMap
* @see SortedSet
* @see Comparator
* @see Comparable
* @see Collection
* @see ClassCastException
* @since 1.2
*/
public interface SortedMap<K,V> extends Map<K,V> {
/**
* Returns the comparator used to order the keys in this map, or
* {@code null} if this map uses the {@linkplain Comparable
* natural ordering} of its keys.
*
* 返回一个比较器,内部使用key的自然大小排序
*
* @return the comparator used to order the keys in this map,
* or {@code null} if this map uses the natural ordering
* of its keys
*/
Comparator<? super K> comparator();
/**
* Returns a view of the portion of this map whose keys range from
* {@code fromKey}, inclusive, to {@code toKey}, exclusive. (If
* {@code fromKey} and {@code toKey} are equal, the returned map
* is empty.) The returned map is backed by this map, so changes
* in the returned map are reflected in this map, and vice-versa.
* The returned map supports all optional map operations that this
* map supports.
*
* 截取Map,返回从fromKey到toKey的映射子集合【左开右闭】
*
* @param fromKey low endpoint (inclusive) of the keys in the returned map
* @param toKey high endpoint (exclusive) of the keys in the returned map
* @return a view of the portion of this map whose keys range from
* {@code fromKey}, inclusive, to {@code toKey}, exclusive
*/
SortedMap<K,V> subMap(K fromKey, K toKey);
/**
* Returns a view of the portion of this map whose keys are
* strictly less than {@code toKey}. The returned map is backed
* by this map, so changes in the returned map are reflected in
* this map, and vice-versa. The returned map supports all
* optional map operations that this map supports.
*
* 返回key值小于toKey的Map映射
*
* @param toKey high endpoint (exclusive) of the keys in the returned map
* @return a view of the portion of this map whose keys are strictly
* less than {@code toKey}
*/
SortedMap<K,V> headMap(K toKey);
/**
* Returns a view of the portion of this map whose keys are
* greater than or equal to {@code fromKey}. The returned map is
* backed by this map, so changes in the returned map are
* reflected in this map, and vice-versa. The returned map
* supports all optional map operations that this map supports.
*
* 返回key值大于fromKey的Map映射
*
* @param fromKey low endpoint (inclusive) of the keys in the returned map
* @return a view of the portion of this map whose keys are greater
* than or equal to {@code fromKey}
*/
SortedMap<K,V> tailMap(K fromKey);
/**
* Returns the first (lowest) key currently in this map.
*
* 返回最小key
*
* @return the first (lowest) key currently in this map
* @throws NoSuchElementException if this map is empty
*/
K firstKey();
/**
* Returns the last (highest) key currently in this map.
*
* 返回最大key
*
* @return the last (highest) key currently in this map
* @throws NoSuchElementException if this map is empty
*/
K lastKey();
/**
*返回key的set集合
*
* @return a set view of the keys contained in this map, sorted in
* ascending order
*/
Set<K> keySet();
/**
* 返回value的Collection集合
*
* @return a collection view of the values contained in this map,
* sorted in ascending key order
*/
Collection<V> values();
/**
* 返回Entry映射集合
*
* @return a set view of the mappings contained in this map,
* sorted in ascending key order
*/
Set<Map.Entry<K, V>> entrySet();
}
NavigableMap
SortedMap接口主要提供了获取最大值与最小值的方法,但对于一个已经排序的数据集,除了最大值与最小值之外,我们还需要些别的功能,更快的,比如获取比某个key值大或者小的值等
NavigableMap继承了SortedMap接口,主要扩展了一些便于匹配的导航方法,且主要是为了查找而不是遍历
/**
* @author Doug Lea
* @author Josh Bloch
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @since 1.6
*/
public interface NavigableMap<K,V> extends SortedMap<K,V> {
/**
* 获取比传入key值小的最大映射
*
* @param key the key
* @return an entry with the greatest key less than {@code key},
* or {@code null} if there is no such key
*/
Map.Entry<K,V> lowerEntry(K key);
/**
* 获取比传入key值小的最大key
*
* @param key the key
* @return the greatest key less than {@code key},
* or {@code null} if there is no such key
*
*/
K lowerKey(K key);
/**
* 返回键值小于等于传入key的最大映射
*
* @param key the key
* @return an entry with the greatest key less than or equal to
* {@code key}, or {@code null} if there is no such key
*
*/
Map.Entry<K,V> floorEntry(K key);
/**
* 返回键值小于等于传入key的最大key值
*
* @param key the key
* @return the greatest key less than or equal to {@code key},
* or {@code null} if there is no such key
*
*/
K floorKey(K key);
/**
* 返回大于等于传入key值的第一个映射
*
* @param key the key
* @return an entry with the least key greater than or equal to
* {@code key}, or {@code null} if there is no such key
*
*/
Map.Entry<K,V> ceilingEntry(K key);
/**
* 返回大于等于传入key的第一个key值
*
* @param key the key
* @return the least key greater than or equal to {@code key},
* or {@code null} if there is no such key
*
*/
K ceilingKey(K key);
/**
* 返回大于传入key的最小映射
*
* @param key the key
* @return an entry with the least key greater than {@code key},
* or {@code null} if there is no such key
*
*/
Map.Entry<K,V> higherEntry(K key);
/**
* 返回大于传入key的最小key值
*
* @param key the key
* @return the least key greater than {@code key},
* or {@code null} if there is no such key
*
*/
K higherKey(K key);
/**
* 获取最小key映射
*
* @return an entry with the least key,
* or {@code null} if this map is empty
*/
Map.Entry<K,V> firstEntry();
/**
* 获取最大key映射
*
* @return an entry with the greatest key,
* or {@code null} if this map is empty
*/
Map.Entry<K,V> lastEntry();
/**
* 删除最小key映射
*
* @return the removed first entry of this map,
* or {@code null} if this map is empty
*/
Map.Entry<K,V> pollFirstEntry();
/**
* 删除最大key映射
*
* @return the removed last entry of this map,
* or {@code null} if this map is empty
*/
Map.Entry<K,V> pollLastEntry();
/**
* 返回倒序Map
*
* @return a reverse order view of this map
*/
NavigableMap<K,V> descendingMap();
/**
* 返回一个key的NavigableSet集合,NavigableSet和NavigableMap类似
*
* @return a navigable set view of the keys in this map
*/
NavigableSet<K> navigableKeySet();
/**
* 对上述集合倒序
*
* @return a reverse order navigable set view of the keys in this map
*/
NavigableSet<K> descendingKeySet();
/**
* 截取Map,返回一个从 fromKey 至 toKey 的子Map,是否包含fromKey和toKey由fromInclusive和toInclusive决定
*
* @param fromKey low endpoint of the keys in the returned map
* @param fromInclusive {@code true} if the low endpoint
* is to be included in the returned view
* @param toKey high endpoint of the keys in the returned map
* @param toInclusive {@code true} if the high endpoint
* is to be included in the returned view
* @return a view of the portion of this map whose keys range from
* {@code fromKey} to {@code toKey}
*
*/
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
K toKey, boolean toInclusive);
/**
* 返回一个键值小于等于fromKey的子Map(inclusive决定是否包含有fromKey)
*
* @param toKey high endpoint of the keys in the returned map
* @param inclusive {@code true} if the high endpoint
* is to be included in the returned view
* @return a view of the portion of this map whose keys are less than
* (or equal to, if {@code inclusive} is true) {@code toKey}
*
*/
NavigableMap<K,V> headMap(K toKey, boolean inclusive);
/**
* 返回一个键值大于等于toKey的子Map(inclusive决定是否包含有toKey)
*
* <p>The returned map will throw an {@code IllegalArgumentException}
* on an attempt to insert a key outside its range.
*
* @param fromKey low endpoint of the keys in the returned map
* @param inclusive {@code true} if the low endpoint
* is to be included in the returned view
* @return a view of the portion of this map whose keys are greater than
* (or equal to, if {@code inclusive} is true) {@code fromKey}
*
*/
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
/**
* 返回一个键值从fromKey到toKey的子Map(左开右闭)
*
* <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedMap<K,V> subMap(K fromKey, K toKey);
/**
* 返回一个键值小于toKey的子Map
*
* <p>Equivalent to {@code headMap(toKey, false)}.
*/
SortedMap<K,V> headMap(K toKey);
/**
* 返回一个键值大于等于fromKey的子Map
*
* <p>Equivalent to {@code tailMap(fromKey, true)}.
*
*/
SortedMap<K,V> tailMap(K fromKey);
}
简单使用
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);
}
}
执行结果:
总结
1、SortedMap直接继承了Map接口,提供了若干个与排序相关的方法,如获取最大最小key值等
2、NavigableMap继承了SortedMap接口,主要扩展了一些便于匹配的导航方法,且主要是为了查找而不是遍历
3、TreeMap实现了NavigableMap,也就相当于实现了SortedMap,是比较常用的有序Map实现。