【发布时间】:2013-07-04 14:59:22
【问题描述】:
来自API:
1)以下是 TreeMap 的构造函数,它接受另一个地图并使用(传递的地图的)可比较接口对其进行排序。
TreeMap public TreeMap(Map<? extends K,? extends V> m) Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys. All插入新地图的键必须实现 Comparable 界面。此外,所有这些键必须是相互可比的: k1.compareTo(k2) 不能为任何键 k1 抛出 ClassCastException 和地图中的k2。此方法在 n*log(n) 时间内运行。
Parameters: m - the map whose mappings are to be placed in this map Throws: ClassCastException - if the keys in m are not Comparable, or are not mutually comparable NullPointerException - if the specified map is null
2) 下面是 TreeMap 的构造函数,它接受另一个地图并使用(传递的地图的)比较器接口对其进行排序。
树图
public TreeMap(SortedMap<K,? extends V> m) Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map. This method runs in线性时间。
Parameters: m - the sorted map whose mappings are to be placed in this map, and whose comparator is to be used to sort this map Throws: NullPointerException - if the specified map is null
为什么第一个签名是public TreeMap(Map<? extends K, ? extends V> m),第二个是public TreeMap(SortedMap<K,? extends V> m)?
更新:如果问题不够清楚,我想知道为什么与构造函数中的 KEYS 参数相关的泛型部分彼此不同。 ? extends K 和 K
【问题讨论】: