SortedMap和TreeMap

接口 SortedMap<wbr style="line-height:25px">&lt;K,V&gt;<br style="line-height:25px"> publicinterfaceSortedMap&lt;K,V&gt;<br style="line-height:25px"> extendsMap&lt;K,V&gt;<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px"></wbr></span><span style="color:#000080; line-height:25px">保证按照键的升序排列的映射,可以按照键的自然顺序(参见Comparable接口)进行排序,或者通过创建有序映射时提供的比较器进行排序<wbr style="line-height:25px">。对有序映射的集合视图</wbr></span><br style="line-height:25px"><span style="color:#003366; line-height:25px">(由entrySet、keySet和values方法返回)进行迭代时,此顺序就会反映出来。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">要采用此排序,还需要提供一些其他操作(此接口是相似于SortedSet接口的映射)。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">插入有序映射的所有键都必须实现Comparable接口(或者被指定的比较器所接受)。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">另外,所有这些键都必须是可互相比较的</span><span style="color:#0000ff; line-height:25px">:k1.compareTo(k2)(或comparator.compare(k1,k2))<br style="line-height:25px"> 对有序映射中的任意两个元素k1和k2都不得抛出ClassCastException。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">试图违反这些限制将导致违反方法或者构造方法的调用,从而抛出ClassCastException。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">注意,如果有序映射正确实现了Map接口,则有序映射所保持的顺序(无论是否明确提供了比较器)都必须保持相等一致性。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">(相等一致性的精确定义请参阅Comparable接口或Comparator接口)。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">这也是因为Map接口是按照equals操作定义的,但有序映射使用它的compareTo(或compare)方法对所有键进行比较,</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">因此</span><span style="color:#000080; line-height:25px"><wbr style="line-height:25px">从有序映射的观点来看,此方法认为两个对象拥有相等的映射键则说明它们是相等的</wbr></span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">即使顺序没有保持相等一致性,树映射的行为仍然是定义良好的,只不过没有遵守Map接口的常规协定。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px"><wbr style="line-height:25px">所有通用有序映射实现类都应该提供4个“标准”构造方法<wbr style="line-height:25px">:</wbr></wbr></span><br style="line-height:25px"><span style="color:#000080; line-height:25px">1)void(不带参数)构造方法,创建空的有序映射,按照键的自然顺序排序。<br style="line-height:25px"> 2)带有一个Comparator类型参数的构造方法,创建一个空的有序映射,根据指定的比较器排序。<br style="line-height:25px"> 3)带有一个Map类型参数的构造方法,创建一个键-值映射关系与参数相同的有序映射,按照键的自然顺序排序。<br style="line-height:25px"> 4)带有一个有序映射类型参数的构造方法,创建一个新的有序映射,键-值映射关系及排序方法与输入的有序映射相同。如</span>:<a target="_blank" rel="nofollow" href="http://download-llnw.oracle.com/javase/6/docs/api/java/util/TreeMap.html#TreeMap%28java.util.SortedMap%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"><span style="line-height:25px"><wbr style="line-height:25px"><span style="font-family:Courier; line-height:1.3em">TreeMap</span><wbr style="line-height:25px"></wbr></wbr></span><wbr style="line-height:25px"></wbr></a><wbr style="line-height:25px"><span style="font-family:Courier; line-height:1.3em">(</span><wbr style="line-height:25px"><a target="_blank" rel="nofollow" href="http://download-llnw.oracle.com/javase/6/docs/api/java/util/SortedMap.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"><span style="font-family:Courier; line-height:1.3em">SortedMap</span><wbr style="line-height:25px"></wbr></a><wbr style="line-height:25px"><span style="font-family:Courier; line-height:1.3em">&lt;</span><wbr style="line-height:25px"><a target="_blank" rel="nofollow" href="http://download-llnw.oracle.com/javase/6/docs/api/java/util/TreeMap.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"><span style="font-family:Courier; line-height:1.3em">K</span><wbr style="line-height:25px"></wbr></a><wbr style="line-height:25px"><span style="font-family:Courier; line-height:1.3em">,?extends</span><wbr style="line-height:25px"><a target="_blank" rel="nofollow" href="http://download-llnw.oracle.com/javase/6/docs/api/java/util/TreeMap.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"><span style="font-family:Courier; line-height:1.3em">V</span><wbr style="line-height:25px"></wbr></a><wbr style="line-height:25px"><span style="font-family:Courier; line-height:1.3em">&gt;m)</span><wbr style="line-height:25px"><br style="line-height:25px"> 除了JDK实现(TreeMap类)遵循此建议外,无法保证强制实施此建议(因为接口不能包含构造方法)。<br style="line-height:25px"> SortedMap声明的接口<br style="line-height:25px"><div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <table id="pubmethods" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th colspan="12" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:rgb(222,232,241)"> Public Methods</th> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">abstract<a rel="nofollow" href="http://developer.android.com/reference/java/util/Comparator.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Comparator</a>&lt;?superK&gt;</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html#comparator()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">comparator</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Returns the comparator used to compare keys in this sorted map.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 返回与此有序映射关联的比较器,如果使用键的自然顺序,则返回 null。</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">abstract K</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html#firstKey()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">firstKey</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Returns the first key in this sorted map.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 返回有序映射中当前第一个(最小的)键。</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">abstract<a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedMap</a>&lt;K,V&gt;</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html#headMap(K)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">headMap</a></span>(K endKey)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Returns a sorted map over a range of this sorted map with all keys that are less than the specified<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">endKey</code>.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 返回此有序映射的部分元素,其元素的键值应该小于 toKey。</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">abstract K</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html#lastKey()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">lastKey</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Returns the last key in this sorted map.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 返回有序映射中当前最后一个(最大的)键。</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">abstract<a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedMap</a>&lt;K,V&gt;</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html#subMap(K,%20K)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">subMap</a></span>(K startKey, K endKey)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Returns a sorted map over a range of this sorted map with all keys greater than or equal to the specified<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">startKey</code>and less than the specified<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">endKey</code>.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 返回此有序映射的部分视图,其键值从 fromKey(包括)到 toKey(不包括)。</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px">abstract<a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedMap</a>&lt;K,V&gt;</nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/SortedMap.html#tailMap(K)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">tailMap</a></span>(K startKey)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Returns a sorted map over a range of this sorted map with all keys that are greater than or equal to the specified<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">startKey</code>.</div> <div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> 返回有序映射的部分视图,其键大于或等于 fromKey。</div> </td> </tr> </tbody></table> <span style="line-height:25px">TreeMap(1.2)<br style="line-height:25px"></span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px"></span><wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">TreeMap</span><span style="color:#003366; line-height:25px">是</span><span style="color:#ff6600; line-height:25px">SortedMap</span><span style="color:#003366; line-height:25px">接口的基于红黑树的实现。此类保证了映射按照升序顺序排列关键字,根据使用的构造方法不同,可能会按照键的类的自然顺序进行排序(参见Comparable),或者按照创建时所提供的比较器进行排序<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">此实现为containsKey、get、put和remove操作提供了保证的</span><span style="color:#ff6600; line-height:25px">log(n)</span><span style="color:#0000ff; line-height:25px">时间开销。</span><br style="line-height:25px"> 这些算法是Cormen、Leiserson和Rivest的《IntroductiontoAlgorithms》中的算法的改编。<br style="line-height:25px"> 注意,如果有序映射要正确实现Map接口,则有序映射所保持的顺序(无论是否明确提供比较器)都必须保持与等号一致。<br style="line-height:25px"> (请参见与等号一致的精确定义的Comparable或Comparator。)这也是因为Map接口是按照等号操作定义的,<br style="line-height:25px"> 但映射使用它的compareTo(或compare)方法对所有键进行比较,因此从有序映射的观点来看,<br style="line-height:25px"> 此方法认为相等的两个键就是相等的。即使顺序与等号不一致,有序映射的行为仍然是定义良好的;<br style="line-height:25px"> 只不过没有遵守Map接口的常规约定。<br style="line-height:25px"> 注意,此实现不是同步的。如果多个线程同时访问一个映射,并且其中至少一个线程从结构上修改了该映射,<br style="line-height:25px"> 则其必须保持外部同步。(结构上修改是指添加或删除一个或多个映射关系的操作;<br style="line-height:25px"> 仅改变与现有键关联的值不是结构上的修改。)这一般通过对自然封装该映射的某个对象进行同步操作来完成。<br style="line-height:25px"> 如果不存在这样的对象,则应该使用Collections.synchronizedMap方法来“包装”该映射。<br style="line-height:25px"> 最好在创建时完成这一操作,以防止对映射进行意外的不同步访问,如下所示:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Mapm=Collections.synchronizedMap(newTreeMap(...));</span><br style="line-height:25px"> 由所有此类的“collection视图方法”所返回的迭代器都是快速失败的:在迭代器创建之后,<br style="line-height:25px"> 如果从结构上对映射进行修改,除非通过迭代器自身的remove或add方法,其他任何时间任何方式的修改,<br style="line-height:25px"> 迭代器都将抛出ConcurrentModificationException。因此,面对并发的修改,迭代器很快就完全失败,<br style="line-height:25px"> 而不是冒着在将来不确定的时间任意发生不确定行为的风险。<br style="line-height:25px"> 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证。<br style="line-height:25px"> 快速失败迭代器会尽最大努力抛出ConcurrentModificationException。<br style="line-height:25px"> 因此,为提高这类迭代器的正确性而编写一个依赖于此异常的程序是错误的做法:迭代器的快速失败行为应该仅用于检测bug。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:此实现不是同步的。不是线程安全的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:TreeMap是用键来进行升序顺序来排序的。通过Comparable或Comparator来排序。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:由所有此类的“collection视图方法”所返回的迭代器都是快速失败的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:和HashMap一样,如果插入重复的元素,后面的元素会覆盖前面的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5:</wbr></span><wbr style="line-height:25px">键不可以为null,但是值可以为null<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">示例1</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Comparator;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Random;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.TreeMap;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.SynchronousQueue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.TimeUnit;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px">Test{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *@paramargs<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">(String[]args){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">TreeMap&lt;Integer,People&gt;map=newTreeMap();<br style="line-height:25px"> Peoplep1=newPeople("robin",1,28);<br style="line-height:25px"> Peoplep2=newPeople("robin",2,29);<br style="line-height:25px"> Peoplep3=newPeople("harry",3,30);<br style="line-height:25px"> map.put(newInteger(100),p1);<br style="line-height:25px"> map.put(newInteger(100),p2);<br style="line-height:25px"> map.put(newInteger(1),p3);<br style="line-height:25px"> map.put(newInteger(2),null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(Peoplep:map.values())</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(p);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">People{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringname;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intid;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intage;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">People(Stringname,intid)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this(name,id,0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">People(Stringname,intid,intage)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.name=name;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.id=id;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.age=age;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">StringtoString()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnid+name+age;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicboolean</span><span style="color:#3366ff; line-height:25px">equals(Objecto)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(o==null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnfalse;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(!(oinstanceofPeople))</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnfalse;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplep=(People)o;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">booleanres=name.equals(p.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(res)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("name"+name+"isdouble");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(name+"vS"+p.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnres;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicint</span><span style="color:#3366ff; line-height:25px">hashCode()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnname.hashCode();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">输出结果</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">3harry30</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">null</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">2robin29</span><br style="line-height:25px"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值