SortedSet(1.5)
SortedSet是个接口,它里面的(只有TreeSet这一个实现可用)中的元素一定是有序的。
保证迭代器按照元素递增顺序遍历的集合,<wbr style="line-height:25px">可以按照元素的自然顺序(参见Comparable)进行排序,或者按照创建有序集合时提供的Comparator进行排序。</wbr>要采用此排序,
还要提供一些其他操作(此接口是SortedMap的集合对应物)。
插入有序集合的所有元素都必须实现Comparable接口(或者被指定的Comparator所接受)。
另外,所有这些元素都必须是可相互比较的:e1.compareTo(e2)
(或comparator.compare(e1,e2))对于有序集合中的任意元素e1和e2都不能抛出ClassCastException。
试图违反这些限制将导致违反规则的方法或者构造方法调用抛出ClassCastException。
注意,如果有序集合正确实现了Set接口,则有序集合所保持的顺序(无论是否明确提供了比较器)
都必须保持相等一致性(相等一致性的精确定义请参阅Comparable接口或Comparator接口)。
这也是因为Set接口是按照equals操作定义的,但有序集合使用它的compareTo(或compare)方法对所有元素进行比较,
因此从有序集合的观点来看,此方法认为相等的两个元素就是相等的。
即使顺序没有保持相等一致性,有序集合的行为仍然是定义良好的,
只不过没有遵守Set接口的常规协定。
<wbr style="line-height:25px">所有通用有序集合实现类都应该提供4个“标准”构造方法</wbr><wbr style="line-height:25px">:<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)带有一个Collection类型参数的构造方法,创建一个元素与参数相同的有序集合,按照元素的自然顺序排序。<br style="line-height:25px"> 4)带有一个SortedSet类型参数的构造方法,创建一个新的有序集合,元素及排序方法与输入的有序集合相同。</span><br style="line-height:25px"> 除了JDK实现(TreeSet类)遵循此建议外,无法保证强制实施此建议(因为接口不能包含构造方法)。<br style="line-height:25px"><br style="line-height:25px"><div style="line-height:25px">声明的主要接口<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><?superE></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/SortedSet.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 elements in this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</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"> 返回与此有序集合关联的比较器,如果使用元素的自然顺序,则返回 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 E</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/SortedSet.html#first()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">first</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 element in this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</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"> 返回此有序集合中当前第一个(最小的)元素。</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/SortedSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedSet</a><E></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/SortedSet.html#headSet(E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">headSet</a></span>(E end)</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<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>of the specified portion of this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>which contains elements less than the end element.</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"> 用一个SortedSet, 返回此有序集合中小于end的所有元素。</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 E</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/SortedSet.html#last()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">last</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 element in this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</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"> 返回此有序集合中最后一个(最大的)元素</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/SortedSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedSet</a><E></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/SortedSet.html#subSet(E,%20E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">subSet</a></span>(E start, E end)</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<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>of the specified portion of this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>which contains elements greater or equal to the start element but less than the end element.</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"> 返回此有序集合的部分元素,元素范围从 fromElement(包括)到 toElement(不包括)。</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/SortedSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedSet</a><E></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/SortedSet.html#tailSet(E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">tailSet</a></span>(E start)</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<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>of the specified portion of this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>which contains elements greater or equal to the start element.</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"> 返回此有序集合的部分元素,其元素大于或等于 fromElement。</div> </td> </tr> </tbody></table> </div> </div> </wbr><wbr style="line-height:25px">注意1</wbr><wbr style="line-height:25px">:SortedSet意思是“根据对象的比较顺序”,而不是“插入顺序”进行排序.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:关于SortedSet的更多信息请参阅下面的它唯一实现类TreeSet。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">TreeSet</wbr></span><br style="line-height:25px"><span style="color:#003366; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">TreeSet</span><span style="color:#003366; line-height:25px">类实现Set接口,该接口由</span><span style="color:#993300; line-height:25px">TreeMap</span><span style="color:#003366; line-height:25px">实例支持<wbr style="line-height:25px">。此类保证排序后的set按照升序排列元素,<br style="line-height:25px"> 根据使用的构造方法不同,可能会按照元素的自然顺序进行排序(参见Comparable),<br style="line-height:25px"> 或按照在创建set时所提供的比较器进行排序。<br style="line-height:25px"> 此实现为基本操作(add、remove和contains)提供了可保证的log(n)时间开销。</wbr></span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">,如果要正确实现Set接口,则set所维护的顺序(是否提供了显式比较器)<br style="line-height:25px"> 必须为与等号一致(请参阅与等号一致精确定义的Comparable或Comparator)。<br style="line-height:25px"> 这是因为Set接口根据equals操作进行定义,但TreeSet实例将使用其compareTo(或compare)方法执行所有的键比较,<br style="line-height:25px"> 因此从set的角度出发,该方法认为相等的两个键就是相等的。即使set的顺序与等号不一致,其行为也是定义良好的;<br style="line-height:25px"> 它只是违背了Set接口的常规协定。<br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">,此实现不是同步的。如果多个线程同时访问一个set,而其中至少一个线程修改了该set,那么它必须保持外部同步<br style="line-height:25px"> 。通常通过对某个自然封装该set的对象进行同步来实现此操作。如果不存在此类对象,<br style="line-height:25px"> 则set就应该使用<span style="color:#0000ff; line-height:25px">Collections.synchronizedSet</span>方法进行“包装”。此操作最好在创建时进行,以防止对set的意外非同步访问:<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px"></wbr></span><span style="color:#0000ff; line-height:25px">SortedSets=Collections.synchronizedSortedSet(newTreeSet(...));</span><wbr style="line-height:25px"><br style="line-height:25px"> 此类的iterator方法返回的迭代器是快速失败的:如果在迭代器创建后的任意时间修改set<br style="line-height:25px"> (通过迭代器本身remove方法之外的任何其他方式),迭代器将抛出ConcurrentModificationException。<br style="line-height:25px"> 因此,在并发修改时,迭代器将快速而彻底地失败,而不会在以后的不确定时间有出现任意、无法确定行为的危险。<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"> 快速失败的迭代器将尽量抛出ConcurrentModificationException。因此,为了获得其准确性而编写依赖此异常的程序的做法是错误的:<br style="line-height:25px"> 迭代器的快速失败行为应当仅用于检测bug。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">1:允许null元素.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2:</wbr></span><wbr style="line-height:25px">实现不同步的,不是线程安全的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3:</wbr></span><wbr style="line-height:25px">TreeSet实例将使用其compareTo(或compare)方法执行所有的键比较,<br style="line-height:25px"> 认为两个对象的键相等就表示它们两个对象是相等的。它违背了Set接口的常规协定。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4:</wbr></span><wbr style="line-height:25px">此类的iterator方法返回的迭代器是快速失败的:在创建迭代器之后,如果对集合进行修改,<br style="line-height:25px"> 除非通过迭代器自身的remove方法,否则在任何时间以任何方式对其进行修改,<br style="line-height:25px"> Iterator都将抛出ConcurrentModificationException<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:iterator()返回的迭代器,里面的元素是以升序排序的.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意6</wbr></span><wbr style="line-height:25px">:当试图添加一个重复元素到TreeSet时,新元素并不会把旧元素替换掉,<br style="line-height:25px"> 而只是新元素不会添加到TreeSet(不会抛异常。)<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意7</wbr></span><wbr style="line-height:25px">:TreeSet用了一种叫红黑树的数据结构【red-blacktreedatastructure】来为元素排序,<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.TreeSet;</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">public</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">class</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:#ff6600; 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">PeopleComparatorcomparator=newPeopleComparator();<br style="line-height:25px"> TreeSet<People>set=newTreeSet(comparator);<br style="line-height:25px"> set.add(newPeople("robin",1,21));<br style="line-height:25px"> set.add(newPeople("hb",2,20));<br style="line-height:25px"> set.add(newPeople("harry",9,30));<br style="line-height:25px"> set.add(null);<br style="line-height:25px"> Peoplep4=newPeople("robin",4,25);<br style="line-height:25px"> set.add(p4);<br style="line-height:25px"> set.add(newPeople("yp",5,28));<br style="line-height:25px"> set.add(newPeople("yp2",8,28));<br style="line-height:25px"> for(Peoplep:set)<br style="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:#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">publicPeople(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">publicStringtoString()</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><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">PeopleComparator</span><span style="color:#993300; line-height:25px">implements</span><span style="color:#3366ff; line-height:25px">Comparator<People></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicintcompare(Peoplep0,Peoplep1){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(p0==p1)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(p0!=null&&p1==null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">elseif(p0==null&&p1!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return-1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(p0.id>p1.id)<br style="line-height:25px"> return1;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(p0.id<p1.id)<br style="line-height:25px"> return-1;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">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> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
SortedSet是个接口,它里面的(只有TreeSet这一个实现可用)中的元素一定是有序的。
保证迭代器按照元素递增顺序遍历的集合,<wbr style="line-height:25px">可以按照元素的自然顺序(参见Comparable)进行排序,或者按照创建有序集合时提供的Comparator进行排序。</wbr>要采用此排序,
还要提供一些其他操作(此接口是SortedMap的集合对应物)。
插入有序集合的所有元素都必须实现Comparable接口(或者被指定的Comparator所接受)。
另外,所有这些元素都必须是可相互比较的:e1.compareTo(e2)
(或comparator.compare(e1,e2))对于有序集合中的任意元素e1和e2都不能抛出ClassCastException。
试图违反这些限制将导致违反规则的方法或者构造方法调用抛出ClassCastException。
注意,如果有序集合正确实现了Set接口,则有序集合所保持的顺序(无论是否明确提供了比较器)
都必须保持相等一致性(相等一致性的精确定义请参阅Comparable接口或Comparator接口)。
这也是因为Set接口是按照equals操作定义的,但有序集合使用它的compareTo(或compare)方法对所有元素进行比较,
因此从有序集合的观点来看,此方法认为相等的两个元素就是相等的。
即使顺序没有保持相等一致性,有序集合的行为仍然是定义良好的,
只不过没有遵守Set接口的常规协定。
<wbr style="line-height:25px">所有通用有序集合实现类都应该提供4个“标准”构造方法</wbr><wbr style="line-height:25px">:<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)带有一个Collection类型参数的构造方法,创建一个元素与参数相同的有序集合,按照元素的自然顺序排序。<br style="line-height:25px"> 4)带有一个SortedSet类型参数的构造方法,创建一个新的有序集合,元素及排序方法与输入的有序集合相同。</span><br style="line-height:25px"> 除了JDK实现(TreeSet类)遵循此建议外,无法保证强制实施此建议(因为接口不能包含构造方法)。<br style="line-height:25px"><br style="line-height:25px"><div style="line-height:25px">声明的主要接口<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><?superE></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/SortedSet.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 elements in this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</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"> 返回与此有序集合关联的比较器,如果使用元素的自然顺序,则返回 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 E</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/SortedSet.html#first()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">first</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 element in this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</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"> 返回此有序集合中当前第一个(最小的)元素。</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/SortedSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedSet</a><E></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/SortedSet.html#headSet(E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">headSet</a></span>(E end)</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<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>of the specified portion of this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>which contains elements less than the end element.</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"> 用一个SortedSet, 返回此有序集合中小于end的所有元素。</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 E</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/SortedSet.html#last()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">last</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 element in this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</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"> 返回此有序集合中最后一个(最大的)元素</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/SortedSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedSet</a><E></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/SortedSet.html#subSet(E,%20E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">subSet</a></span>(E start, E end)</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<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>of the specified portion of this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>which contains elements greater or equal to the start element but less than the end element.</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"> 返回此有序集合的部分元素,元素范围从 fromElement(包括)到 toElement(不包括)。</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/SortedSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">SortedSet</a><E></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/SortedSet.html#tailSet(E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">tailSet</a></span>(E start)</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<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>of the specified portion of this<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">SortedSet</code>which contains elements greater or equal to the start element.</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"> 返回此有序集合的部分元素,其元素大于或等于 fromElement。</div> </td> </tr> </tbody></table> </div> </div> </wbr><wbr style="line-height:25px">注意1</wbr><wbr style="line-height:25px">:SortedSet意思是“根据对象的比较顺序”,而不是“插入顺序”进行排序.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:关于SortedSet的更多信息请参阅下面的它唯一实现类TreeSet。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">TreeSet</wbr></span><br style="line-height:25px"><span style="color:#003366; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">TreeSet</span><span style="color:#003366; line-height:25px">类实现Set接口,该接口由</span><span style="color:#993300; line-height:25px">TreeMap</span><span style="color:#003366; line-height:25px">实例支持<wbr style="line-height:25px">。此类保证排序后的set按照升序排列元素,<br style="line-height:25px"> 根据使用的构造方法不同,可能会按照元素的自然顺序进行排序(参见Comparable),<br style="line-height:25px"> 或按照在创建set时所提供的比较器进行排序。<br style="line-height:25px"> 此实现为基本操作(add、remove和contains)提供了可保证的log(n)时间开销。</wbr></span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">,如果要正确实现Set接口,则set所维护的顺序(是否提供了显式比较器)<br style="line-height:25px"> 必须为与等号一致(请参阅与等号一致精确定义的Comparable或Comparator)。<br style="line-height:25px"> 这是因为Set接口根据equals操作进行定义,但TreeSet实例将使用其compareTo(或compare)方法执行所有的键比较,<br style="line-height:25px"> 因此从set的角度出发,该方法认为相等的两个键就是相等的。即使set的顺序与等号不一致,其行为也是定义良好的;<br style="line-height:25px"> 它只是违背了Set接口的常规协定。<br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">,此实现不是同步的。如果多个线程同时访问一个set,而其中至少一个线程修改了该set,那么它必须保持外部同步<br style="line-height:25px"> 。通常通过对某个自然封装该set的对象进行同步来实现此操作。如果不存在此类对象,<br style="line-height:25px"> 则set就应该使用<span style="color:#0000ff; line-height:25px">Collections.synchronizedSet</span>方法进行“包装”。此操作最好在创建时进行,以防止对set的意外非同步访问:<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px"></wbr></span><span style="color:#0000ff; line-height:25px">SortedSets=Collections.synchronizedSortedSet(newTreeSet(...));</span><wbr style="line-height:25px"><br style="line-height:25px"> 此类的iterator方法返回的迭代器是快速失败的:如果在迭代器创建后的任意时间修改set<br style="line-height:25px"> (通过迭代器本身remove方法之外的任何其他方式),迭代器将抛出ConcurrentModificationException。<br style="line-height:25px"> 因此,在并发修改时,迭代器将快速而彻底地失败,而不会在以后的不确定时间有出现任意、无法确定行为的危险。<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"> 快速失败的迭代器将尽量抛出ConcurrentModificationException。因此,为了获得其准确性而编写依赖此异常的程序的做法是错误的:<br style="line-height:25px"> 迭代器的快速失败行为应当仅用于检测bug。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">1:允许null元素.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2:</wbr></span><wbr style="line-height:25px">实现不同步的,不是线程安全的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3:</wbr></span><wbr style="line-height:25px">TreeSet实例将使用其compareTo(或compare)方法执行所有的键比较,<br style="line-height:25px"> 认为两个对象的键相等就表示它们两个对象是相等的。它违背了Set接口的常规协定。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4:</wbr></span><wbr style="line-height:25px">此类的iterator方法返回的迭代器是快速失败的:在创建迭代器之后,如果对集合进行修改,<br style="line-height:25px"> 除非通过迭代器自身的remove方法,否则在任何时间以任何方式对其进行修改,<br style="line-height:25px"> Iterator都将抛出ConcurrentModificationException<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:iterator()返回的迭代器,里面的元素是以升序排序的.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意6</wbr></span><wbr style="line-height:25px">:当试图添加一个重复元素到TreeSet时,新元素并不会把旧元素替换掉,<br style="line-height:25px"> 而只是新元素不会添加到TreeSet(不会抛异常。)<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意7</wbr></span><wbr style="line-height:25px">:TreeSet用了一种叫红黑树的数据结构【red-blacktreedatastructure】来为元素排序,<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.TreeSet;</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">public</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">class</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:#ff6600; 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">PeopleComparatorcomparator=newPeopleComparator();<br style="line-height:25px"> TreeSet<People>set=newTreeSet(comparator);<br style="line-height:25px"> set.add(newPeople("robin",1,21));<br style="line-height:25px"> set.add(newPeople("hb",2,20));<br style="line-height:25px"> set.add(newPeople("harry",9,30));<br style="line-height:25px"> set.add(null);<br style="line-height:25px"> Peoplep4=newPeople("robin",4,25);<br style="line-height:25px"> set.add(p4);<br style="line-height:25px"> set.add(newPeople("yp",5,28));<br style="line-height:25px"> set.add(newPeople("yp2",8,28));<br style="line-height:25px"> for(Peoplep:set)<br style="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:#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">publicPeople(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">publicStringtoString()</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><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">PeopleComparator</span><span style="color:#993300; line-height:25px">implements</span><span style="color:#3366ff; line-height:25px">Comparator<People></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicintcompare(Peoplep0,Peoplep1){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(p0==p1)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(p0!=null&&p1==null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">elseif(p0==null&&p1!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return-1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(p0.id>p1.id)<br style="line-height:25px"> return1;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(p0.id<p1.id)<br style="line-height:25px"> return-1;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">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> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>