TreeSet基于TreeMap实现,支持排序;
TreeSet是非线程安全的;
public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, java.io.Serializable
{
/**
* The backing map.
*/
private transient NavigableMap<E,Object> m;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
/**
* Constructs a set backed by the specified navigable map.
*/
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}
/**
* Constructs a new, empty tree set, sorted according to the
* natural ordering of its elements. All elements inserted into
* the set must implement the {@link Comparable} interface.
* Furthermore, all such elements must be <i>mutually
* comparable</i>: {@code e1.compareTo(e2)} must not throw a
* {@code ClassCastException} for any elements {@code e1} and
* {@code e2} in the set. If the user attempts to add an element
* to the set that violates this constraint (for example, the user
* attempts to add a string element to a set whose elements are
* integers), the {@code add} call will throw a
* {@code ClassCastException}.
*/
public TreeSet() {
this(new TreeMap<E,Object>());
}
/**
* Adds the specified element to this set if it is not already present.
* More formally, adds the specified element <tt>e</tt> to this set if
* this set contains no element <tt>e2</tt> such that
* <tt>(e==null ? e2==null : e.equals(e2))</tt>.
* If this set already contains the element, the call leaves the set
* unchanged and returns <tt>false</tt>.
*/
public boolean add(E e) {
return m.put(e, PRESENT)==null;
}
/**
* Removes the specified element from this set if it is present.
* More formally, removes an element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>,
* if this set contains such an element. Returns <tt>true</tt> if
* this set contained the element (or equivalently, if this set
* changed as a result of the call). (This set will not contain the
* element once the call returns.)
*/
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
分享到:
相关推荐
title: TreeSet 和 TreeMap 理论基础Java 根据红黑树这种平衡的二叉搜索树实现了 TreeSet 和 TreeMap 两种数据结构,如果
treemap treeset hashset hashmap 简要介绍
主要为大家详细介绍了java中treemap和treeset实现红黑树,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java中AVL平衡二叉树实现Map_(仿照TreeMap和TreeSet)1
通过TreeSet类直接对学生成绩实现了排序功能,不必要进行相关额外的排序来实现!
文档摘录 文档摘录文档摘录文档摘录文档摘录文档摘录文档摘录
介绍TreeSet集合用法,向TreeSet集合中添加类的对象,此类需实现Comparable接口,有实例,供需要的朋友下载学习。
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
下面小编就为大家带来一篇浅谈java中的TreeMap 排序与TreeSet 排序。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
图书馆管理 1.描述一个图书馆 2.进书,借书卡办理 3.借书,还书 4.查询某书的借出记录 5.查询借书卡的借出记录 6.显示图书列表(按借出次数排序)
JAVA集合框架之List、Map、Set之间的选择~小案例分析:http://blog.csdn.net/qq_23473123/article/details/51240739
TreeSet元素和TreeMap键值要实现排序功能,所以不能为null,否则会抛出运行时异常。
数据结构java版 pdf ,书中有霍夫曼压缩、Dijkstra最小路径算法,平衡搜索树,八皇后问题,RSA加密算法、TreeSet、TreeMap、队列、优先队列、堆栈等算法
Java数据结构--13.Java8数据结构TreeSet 前⾔ ,上⼀篇中对 Set 接⼝最终实现类 HashSet 与 LinkedHashSet 做了介绍与分析,本篇将对另⼀种 Set 接⼝的最终实现类 TreeSet 进⾏ 介绍与分析。 先来看下 TreeSet 完整...
TreeSet 是 Java 中的一个集合类,它实现了 SortedSet 接口,并且使用红黑树作为底层数据结构。TreeSet 具有以下主要特点: 排序性:TreeSet 中的元素是有序的,默认按照元素的自然顺序进行排序。或者,可以在创建 ...
structure 跳表 vs 红黑树,对比了两种数据结构的优劣 跳表(Skip List)是一种基于并行...这使得红黑树成为一种广泛应用于高性能数据结构的选择,比如在标准库中的集合类实现中,如 Java 中的 TreeSet 和 TreeMap。
集合比较 一种。 使用Java的HashSet时,数据结构的初始容量必须小于条目总数/ Load Factor。... 其次,当在TreeSet中实现set接口时,将实现TreeMap的接口。 我们可以说TreeSet是由TreeMap实例实现的。 testrun.tx
毕向东Java基础视频教程-集合框架(TreeSet练习).
概述: 在java集合中,TreeSet集合和TreeMap集合底层数据结构都是自平衡二叉树,所以在这两个集合中添加元素的时候会实现自动排序,排序方式为中序排序(即左根右的方式进行排序,详情请见二叉树数据结构,这里不做...
HashSet和TreeSet_围墙之外.rar