Java SortedSet 接口
该SortedSet的接口扩展集Set并声明了一组按升序排序的行为。
除了通过集定义的那些方法中,SortedSet接口声明总结于下表的方法:
| 序号 | 方法描述 |
|---|---|
| 1 | Comparator comparator( ) 返回调用有序set的比较。如果自然顺序用于此set,则返回null。 |
| 2 | Object first( ) 返回调用排序集合的第一个元素。 |
| 3 | SortedSet headSet(Object end) 返回包含包含在调用的有序set的元素小于结束一个SortedSet。在返回的有序set的元素也被调用的有序set引用。 |
| 4 | Object last( ) 返回调用排序集合的最后一个元素。 |
| 5 | SortedSet subSet(Object start, Object end) 返回一个SortedSet,包括开始和end-1之间的那些元素。被返回的集合中的元素也被调用对象引用。 |
| 6 | SortedSet tailSet(Object start) 返回包含大于或等于启动包含在有序集合这些元素的SortedSet。在返回的集合中的元素也被调用对象的引用。 |
有几种方法没有任何条目包含在调用Set的时候抛出一个NoSuchElementException异常。当一个对象是一个集合中的元素不兼容抛出一个ClassCastException异常。
如果试图使用一个空对象和空不是在Set允许的,一个NullPointerException异常被抛出。
实例
SortedSet有在各种类的实现,如TreeSet,下面是例子一个TreeSet类:
public class SortedSetTest {
public static void main(String[] args) {
// Create the sorted set
SortedSet set = new TreeSet();
// Add elements to the set
set.add("b");
set.add("c");
set.add("a");
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
}
}
以上实例编译运行结果如下:
a b c

国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码













