Java集合-TreeSet

介绍

  1. TreeSet实现了NavigableSet接口,TreeSet的底层是一个NavigableMap,TreeSet的元素是有序的;
  2. TreeSet不允许添加null元素(NullPointerException),不允许添加重复元素,即第二次添加重复元素会失败,add(E e)函数返回false;
  3. TreeSet是非线程同步的,是不安全的,可采用如下方法使得TreeSet线程安全:SortedSet s = Collections.synchronizedSortedSet(new TreeSet(…));
  4. TreeSet是基于TreeMap实现的,TreeSet的元素支持2种排序方式:自然排序或者根据提供的Comparator进行排序。

类图

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();

private static final long serialVersionUID = -2479143000061671589L;

构造函数

/**
 * 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. 
 */
public TreeSet() {
    this(new TreeMap<E,Object>());
}

/**
 * Constructs a new, empty tree set, sorted according to the specified
 * comparator.  
 */
public TreeSet(Comparator<? super E> comparator) {
    this(new TreeMap<>(comparator));
}

/**
 * Constructs a new tree set containing the elements in the specified
 * collection, sorted according to the <i>natural ordering</i> of its
 * elements. 
 */
public TreeSet(Collection<? extends E> c) {
    this();
    addAll(c);
}

/**
 * Constructs a new tree set containing the same elements and
 * using the same ordering as the specified sorted set.
 */
public TreeSet(SortedSet<E> s) {
    this(s.comparator());
    addAll(s);
}

常用方法

/**
 * Returns the number of elements in this set (its cardinality).
 */
public int size() {
    return m.size();
}

/**
 * Returns {@code true} if this set contains no elements.
 */
public boolean isEmpty() {
    return m.isEmpty();
}
/**
 * Returns {@code true} if this set contains the specified element.
 */
public boolean contains(Object o) {
    return m.containsKey(o);
}
/**
 * Adds the specified element to this set if it is not already present.
 */
public boolean add(E e) {
    return m.put(e, PRESENT)==null;
}

    /**
 * Removes the specified element from this set if it is present.
 */
public boolean remove(Object o) {
    return m.remove(o)==PRESENT;
}

/**
 * Removes all of the elements from this set.
 * The set will be empty after this call returns.
 */
public void clear() {
    m.clear();
}

/**
 * Returns a shallow copy of this {@code TreeSet} instance. (The elements
 * themselves are not cloned.)
 *
 * @return a shallow copy of this set
 */
@SuppressWarnings("unchecked")
public Object clone() {
    TreeSet<E> clone;
    try {
        clone = (TreeSet<E>) super.clone();
    } catch (CloneNotSupportedException e) {
        throw new InternalError(e);
    }

    clone.m = new TreeMap<>(m);
    return clone;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值