SortedSet和TreeSet
来源:互联网 发布:知远防务网站 编辑:程序博客网 时间:2024/05/18 20:08
SortedSet(1.5)
SortedSet是个接口,它里面的(只有TreeSet这一个实现可用)中的元素一定是有序的。保证迭代器按照元素递增顺序遍历的集合,
还要提供一些其他操作(此接口是 SortedMap 的集合对应物)。
插入有序集合的所有元素都必须实现 Comparable 接口(或者被指定的 Comparator 所接受)。
另外,所有这些元素都必须是可相互比较的:e1.compareTo(e2)
(或 comparator.compare(e1, e2))对于有序集合中的任意元素 e1 和 e2 都不能抛出 ClassCastException。
试图违反这些限制将导致违反规则的方法或者构造方法调用抛出 ClassCastException。
注意,如果有序集合正确实现了 Set 接口,则有序集合所保持的顺序(无论是否明确提供了比较器)
都必须保持相等一致性(相等一致性 的精确定义请参阅 Comparable 接口或 Comparator 接口)。
这也是因为 Set 接口是按照 equals 操作定义的,但有序集合使用它的 compareTo(或 compare)方法对所有元素进行比较,
因此从有序集合的观点来看,此方法认为相等的两个元素就是相等的。
即使顺序没有保持相等一致性,有序集合的行为仍然是 定义良好的,
只不过没有遵守 Set 接口的常规协定。
1) void(不带参数)构造方法,创建空的有序集合,按照元素的自然顺序 排序。
2) 带有一个 Comparator 类型参数的构造方法,创建一个空的有序集合,根据指定的比较器排序。
3) 带有一个 Collection 类型参数的构造方法,创建一个元素与参数相同的有序集合,按照元素的自然顺序排序。
4) 带有一个 SortedSet 类型参数的构造方法,创建一个新的有序集合,元素及排序方法与输入的有序集合相同。
除了 JDK 实现(TreeSet 类)遵循此建议外,无法保证强制实施此建议(因为接口不能包含构造方法)。
声明的主要接口
Public Methods abstract Comparator<? super E> comparator() abstract E first() abstract SortedSet<E> headSet(E end) abstract E last() abstract SortedSet<E> subSet(E start, E end) abstract SortedSet<E> tailSet(E start)
Returns the comparator used to compare elements in this
SortedSet
.返回与此有序集合关联的比较器,如果使用元素的自然顺序,则返回 null。
Returns the first element in this
SortedSet
.返回此有序集合中当前第一个(最小的)元素。
Returns a
SortedSet
of the specified portion of this SortedSet
which contains elements less than the end element.用一个SortedSet, 返回此有序集合中小于end的所有元素。
Returns the last element in this
SortedSet
.返回此有序集合中最后一个(最大的)元素
Returns a
SortedSet
of the specified portion of this SortedSet
which contains elements greater or equal to the start element but less than the end element.返回此有序集合的部分元素,元素范围从 fromElement(包括)到 toElement(不包括)。
Returns a
SortedSet
of the specified portion of this SortedSet
which contains elements greater or equal to the start element.返回此有序集合的部分元素,其元素大于或等于 fromElement。
TreeSet类实现Set 接口,该接口由TreeMap 实例支持
根据使用的构造方法不同,可能会按照元素的自然顺序 进行排序(参见 Comparable),
或按照在创建 set 时所提供的比较器进行排序。
此实现为基本操作(add、remove 和 contains)提供了可保证的 log(n) 时间开销。
必须为与等号一致(请参阅与等号一致 精确定义的 Comparable 或 Comparator)。
这 是因为 Set 接口根据 equals 操作进行定义,但 TreeSet 实例将使用其 compareTo(或 compare)方法执行所有的键比较,
因此 从 set 的角度出发,该方法认为相等的两个键就是相等的。即使 set 的顺序与等号不一致,其行为也是 定义良好的;
它只是违背了 Set 接口的常规协定。
。通常通过对某个自然封装该 set 的对象进行同步来实现此操作。如果不存在此类对象,
则 set 就应该使用Collections.synchronizedSet 方法进行“包装”。此操作最好在创建时进行,以防止对 set 的意外非同步访问:
此类的 iterator 方法返回的迭代器是快速失败的:如果在迭代器创建后的任意时间修改 set
(通过迭代器本身 remove 方法之外的任何其他方式),迭代器将抛出 ConcurrentModificationException。
因此,在并发修改时,迭代器将快速而彻底地失败,而不会在以后的不确定时间有出现任意、无法确定行为的危险。
快速失败的迭代器将尽量抛出 ConcurrentModificationException。因此,为了获得其准确性而编写依赖此异常的程序的做法是错误的:
迭代器的快速失败行为应当仅用于检测 bug。
认为两个对象的键相等就表示它们两个对象是相等的。它违背了 Set 接口的常规协定。
除非通过迭代器自身的 remove 方法,否则在任何时间以任何方式对其进行修改,
Iterator 都将抛出 ConcurrentModificationException
而只是新元素不会添加到TreeSet(不会抛异常。)
import java.util.Comparator;
import java.util.Random;
import java.util.TreeSet;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
PeopleComparator comparator=new PeopleComparator();
TreeSet<People> set=new TreeSet(comparator);
set.add(new People("robin",1,21));
set.add(new People("hb",2,20));
set.add(new People("harry",9,30));
set.add(null);
People p4=new People("robin",4,25);
set.add(p4);
set.add(new People("yp",5,28));
set.add(new People("yp2",8,28));
for(People p:set)
System.out.println(p);
}
}
class People{
String name;
int id;
int age;
public People(String name,int id)
{
this(name,id,0);
}
public People(String name,int id,int age)
{
this.name=name;
this.id=id;
this.age=age;
}
public String toString()
{
return id+name+age;
}
}
class PeopleComparator implements Comparator<People>
{
@Override
public int compare(People p0, People p1) {
if(p0==p1)
return 0;
if(p0!=null&&p1==null)
return 1;
else if(p0==null&&p1!=null)
return -1;
if (p0.id>p1.id)
return 1;
else if (p0.id<p1.id)
return -1;
else
return 0;
}
}
http://blog.csdn.net/hudashi/article/details/6943522
1 0
- SortedSet和TreeSet
- SortedSet和TreeSet
- SortedSet和TreeSet
- SortedSet和TreeSet
- SortedSet和TreeSet
- leetcode 220. Contains Duplicate III TreeSet和SortedSet的应用
- [疯狂Java]集合:SortedSet、TreeSet
- Java-Collection源码分析(十四)——SortedSet、NavigableSet和TreeSet
- SortedSet与SortedMap,TreeSet与TreeMap
- Java SortedSet TreeSet按指定方式排序 不同方式排序
- Java类集--Set接口、HashSet、TreeSet、SortedSet接口
- SortedSet接口与TreeSet实现类(一)
- SortedSet接口与TreeSet实现类(二)
- Java散列集Set 树集TreeSet HashSet LinkedHashSet SortedSet NavigableSet
- Java类集框架——Set接口和子接口SortedSet以及SortedSet接口的子类TreeSet之间的联系和使用、排序设置重复元素的说明
- sortedset
- SortedSet
- HashSet和SortedSet 集合泛型类
- scp 和 ssh 关于端口号的参数的区别
- 4.5电子邮件系统基本知识重点内容总结
- nodejs+edatagrid读取本地excel表格
- 算法竞赛入门经典(第2版)2.5注解与习题
- POJ 2513 Colored Sticks (Trie + 并查集 + 欧拉通路)
- SortedSet和TreeSet
- Sqlserver 中必须使用分号的两种情况--【叶子】
- Android
- Dubbo服务再暴露
- BZOJ 1266: [AHOI2006]上学路线route Floyd算法,网络最小割
- 非旋转Treap-总结
- 剑指offer(C++)——对称的二叉树
- httpd: apr_sockaddr_info_get() failed for bogon
- jvm参数在哪里设置