源码分析之TreeSet

TreeSet简介
public class MyTreeSet<E> 
    extends AbstractSet<E> 
    implements NavigableSet<E>, Cloneable, Serializable {
}

TreeSet继承了AbstractSet抽象类且实现了NavigableSet接口,是一个有序的集合,基于TreeMap实现,支持自然排序或者提供的Comparator进行排序。

NavigableSet简介
public interface NavigableSet<E> extends SortedSet<E> {
    /** 返回小于给定元素e的最大元素,如果没有则返回NULL。 **/
    E lower(E e);

    /** 返回小于或等于给定元素e的最大元素,如果没有则返回NULL。 **/
    E floor(E e);

    /** 返回大于或等于给定元素e的最小元素,如果没有则返回NULL。 **/
    E ceiling(E e);

    /** 返回大于给定元素e的最小元素,如果没有则返回NULL。 **/
    E higher(E e);

    /** 检索并删除第一个最小元素,如果没有则返回NULL。 **/
    E pollFirst();

    /** 检索并删除第后一个最大元素,如果没有则返回NULL。 **/
    E pollLast();

    /** 返回fromElement和toElement之间的元素 可设置是否包含fromElement和toElement **/
    NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                           E toElement,   boolean toInclusive);

    /** 返回toElement之前的元素 可设置是否包含toElement元素 **/
    NavigableSet<E> headSet(E toElement, boolean inclusive);

    /** 返回fromElement之后的元素 可设置包含fromElement元素 **/
    NavigableSet<E> tailSet(E fromElement, boolean inclusive);

    /** 返回fromElement和toElement之间的元素 包含fromElement但不包含toElement **/
    SortedSet<E> subSet(E fromElement, E toElement);

    /** 返回toElement之前的元素 不包含toElement元素 **/
    SortedSet<E> headSet(E toElement);

    /** 返回fromElement之后的元素 包含fromElement元素 **/
    SortedSet<E> tailSet(E fromElement);
}

NavigableSet接口继承了SortedSet接口,最后三个接口继承自SortedSet

SortedSet简介
public interface SortedSet<E> extends Set<E> {

    /** 返回当前集合元素排序的比较器 **/
    Comparator<? super E> comparator();

    /** 返回fromElement和toElement之间的元素 包含fromElement但不包含toElement **/
    SortedSet<E> subSet(E fromElement, E toElement);

    /** 返回toElement之前的元素 不包含toElement元素 **/
    SortedSet<E> headSet(E toElement);

    /** 返回fromElement之后的元素 包含fromElement元素 **/
    SortedSet<E> tailSet(E fromElement);

    /** 返回第一个元素 **/
    E first();

    /** 返回最后一个元素 **/
    E last();
}
TreeSet成员变量
/** 具体存储元素的NavigableMap **/
private transient NavigableMap<E,Object> m;

private static final Object PRESENT = new Object();
TreeSet构造函数
MyTreeSet(NavigableMap<E,Object> m) {
    this.m = m;
}

/** 默认构造函数  基于TreeMap实现 **/
public MyTreeSet() {
    this(new TreeMap<>());
}

/** 提供排序方法的构造函数 **/
public MyTreeSet(Comparator<? super E> comparator) {
    this(new TreeMap<>(comparator));
}

/** 根据给定集合构造函数 **/
public MyTreeSet(Collection<? extends E> c) {
    this();
    addAll(c);
}

/** 根据给定排序的集合构造函数 **/
public MyTreeSet(SortedSet<E> s) {
    this(s.comparator());
    addAll(s);
}
元素添加
/** 添加单个元素 **/
public boolean add(E e) {
    return m.put(e, PRESENT)==null;
}

/** 添加集合元素 **/
public boolean addAll(Collection<? extends E> c) {
    if (m.size()==0 && c.size() > 0 &&
            c instanceof SortedSet &&
            m instanceof TreeMap) {
        SortedSet<? extends E> set = (SortedSet<? extends E>) c;
        TreeMap<E,Object> map = (TreeMap<E, Object>) m;
        Comparator<?> cc = set.comparator();
        Comparator<? super E> mc = map.comparator();
        if (cc==mc || (cc != null && cc.equals(mc))) {
            map.addAllForTreeSet(set, PRESENT);
            return true;
        }
    }
    return super.addAll(c);
}
元素移除
/** 移除单个元素 **/
public boolean remove(Object o) {
    return m.remove(o)==PRESENT;
}

/** 移除当前集合所有元素 **/
public void clear() {
    m.clear();
}
元素查找
/** 是否包含元素 **/
public boolean contains(Object o) {
    return m.containsKey(o);
}

/** 返回集合的长度 **/
public int size() {
    return m.size();
}

/** 返回集合是否为空 **/
public boolean isEmpty() {
    return m.isEmpty();
}
基于SortedSet的元素查找
public Comparator<? super E> comparator() {
    return m.comparator();
}

public SortedSet<E> subSet(E fromElement, E toElement) {
    return subSet(fromElement, true, toElement, false);
}

public SortedSet<E> headSet(E toElement) {
    return headSet(toElement, false);
}

public SortedSet<E> tailSet(E fromElement) {
    return tailSet(fromElement, true);
}

public E first() {
    return m.firstKey();
}

public E last() {
    return m.lastKey();
}
基于NavigableSet的元素查找
 public E lower(E e) {
    return m.lowerKey(e);
}

public E floor(E e) {
    return m.floorKey(e);
}

public E ceiling(E e) {
    return m.ceilingKey(e);
}

public E higher(E e) {
    return m.higherKey(e);
}

public E pollFirst() {
    Map.Entry<E, ?> e = m.pollFirstEntry();
    return e == null ? null : e.getKey();
}

public E pollLast() {
    Map.Entry<E, ?> e = m.pollLastEntry();
    return e == null ? null : e.getKey();
}

public Iterator<E> iterator() {
    return m.navigableKeySet().iterator();
}

public NavigableSet<E> descendingSet() {
    return m.descendingKeySet();
}

public Iterator<E> descendingIterator() {
    return m.navigableKeySet().descendingIterator();
}

public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
    return new TreeSet<>(m.subMap(fromElement, fromInclusive, toElement, toInclusive));
}

public NavigableSet<E> headSet(E toElement, boolean inclusive) {
    return new TreeSet<>(m.headMap(toElement, inclusive));
}
    
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
    return new TreeSet<>(m.tailMap(fromElement, inclusive));
}

转载于:https://my.oschina.net/qgmzzmn/blog/3100935

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值