Estrutura de Dados - TreeSet

breve introdução

TreeSet descreve uma variante do conjunto pode ser conseguido triagem funções são definidos, ele é adicionado ao elemento de falar automaticamente de acordo com uma determinada regra de comparar a sequência a ser inserida na coleção ordenada quando na verdade é o encapsulamento de um TreeMap, como HashSet, LinkedHashSet, que utiliza a chave TreeMap.

classe TreeSet
public class TreeSet<E> extends AbstractSet<E>
        implements NavigableSet<E>, Cloneable, java.io.Serializable

herança AbstractSet classe abstrata que implementa NavigableSet interface

public interface NavigableSet<E> extends SortedSet<E>

NavigableSet estendida SortedSet, você pode obter um elemento mais próximo. Método inferior, chão, tecto, respectivamente, e maior rendimento inferior a, menos do que ou igual a, maior do que ou igual a, maior do que um dado elemento a elemento, se este elemento não está presente, será retornado nulo.

// 返回此set中小于给定元素的最大元素,如果没有这样的元素,则返回NULL。
E lower(E e);
// 返回此集合中小于或等于给定元素的最大元素,如果没有这样的元素,则返回NULL。
E floor(E e);
// 返回此set中大于或等于给定元素的最小元素,如果没有这样的元素,则返回NULL。
E ceiling(E e);
// 返回此set中大于给定元素的最小元素,如果没有这样的元素,则返回NULL。
E higher(E e);
// 检索并删除第一个最小元素。如果没有这样的元素,则返回null。
E pollFirst();
// 检索并删除最后一个最高元素。如果没有这样的元素,则返回null。
E pollLast();
// 迭代器
Iterator<E> iterator();
// 返回此 set 中所包含元素的逆序视图。
NavigableSet<E> descendingSet();
// 以降序返回在此 set 的元素上进行迭代的迭代器。
Iterator<E> descendingIterator();
// 返回此 set 的部分视图,其元素范围从 fromElement 到 toElement。
NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                       E toElement,   boolean toInclusive);
// 返回此 set 的部分视图,其元素小于(或等于,如果 inclusive 为 true)toElement。
NavigableSet<E> headSet(E toElement, boolean inclusive);
//  返回此 set 的部分视图,其元素大于(或等于,如果 inclusive 为 true)fromElement。
NavigableSet<E> tailSet(E fromElement, boolean inclusive);
// 返回此 set 的部分视图,其元素从 fromElement(包括)到 toElement(不包括)。
SortedSet<E> subSet(E fromElement, E toElement);
// 返回此 set 的部分视图,其元素严格小于 toElement。
SortedSet<E> headSet(E toElement);
// 返回此 set 的部分视图,其元素大于(或等于,如果 inclusive 为 true)fromElement。
SortedSet<E> tailSet(E fromElement);
SortedSet interface
public interface SortedSet<E> extends Set<E>

SortedSet classificados de acordo com os elementos do comparador, se o comparador é, em seguida, a ordem natural vazia

SortedSet método
// 获取比较器
Comparator<? super E> comparator();
// 获取两个元素直接的元素集
SortedSet<E> subSet(E fromElement, E toElement);
// 获取之前的元素
SortedSet<E> headSet(E toElement);
// 获取之后的元素
SortedSet<E> tailSet(E fromElement);
// 第一个元素
E first();
// 最后一个元素
E last();
propriedade TreeSet
// NavigableMap对象(NavigableMap下就一个TreeMap实现)
private transient NavigableMap<E,Object> m;
// 固定常量作为Map值
private static final Object PRESENT = new Object();

NavigableMap pode ver artigos treemap

construtor TreeSet
TreeSet(NavigableMap<E,Object> m) {
    this.m = m;
}
public TreeSet() {
    this(new TreeMap<E,Object>());
}
public TreeSet(Comparator<? super E> comparator) {
    this(new TreeMap<>(comparator));
}
public TreeSet(Collection<? extends E> c) {
    this();
    addAll(c);
}
public TreeSet(SortedSet<E> s) {
    this(s.comparator());
    addAll(s);
}

TreeSet está realmente na TreeMap, estrutura interna TreeMap é uma árvore rubro-negro, é apenas a chave aqui, o valor constante fixa

abordagem baseada TreeSet
// 长度
public int size() {
    return m.size();
}
// 是否为空
public boolean isEmpty() {
    return m.isEmpty();
}
// 是否存在
public boolean contains(Object o) {
    return m.containsKey(o);
}
// 添加
public boolean add(E e) {
    return m.put(e, PRESENT)==null;
}
// 删除
public boolean remove(Object o) {
    return m.remove(o)==PRESENT;
}
// 清空
public void clear() {
    m.clear();
}

Branco disse que a chave é usar um TreeMap

Pesquisa TreeSet
// 第一个元素
public E first() {
    return m.firstKey();
}
// 最后一个元素
public E last() {
    return m.lastKey();
}
// 获取最接近e的节点(小于等于)
public E lower(E e) {
    return m.lowerKey(e);
}
// 获取最接近e的节点(大于等于)
public E floor(E e) {
    return m.floorKey(e);
}

Pesquisas TreeMap TreeSet são pesquisa rubro-negro baseado em árvore, outros métodos podem consultar artigos treemap.

Acho que você gosta

Origin www.cnblogs.com/yuanjiangnan/p/12641321.html
Recomendado
Clasificación