千家信息网

TreeSet的介绍和使用

发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,这篇文章主要介绍"TreeSet的介绍和使用",在日常操作中,相信很多人在TreeSet的介绍和使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"TreeSet的介绍和
千家信息网最后更新 2025年02月08日TreeSet的介绍和使用

这篇文章主要介绍"TreeSet的介绍和使用",在日常操作中,相信很多人在TreeSet的介绍和使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"TreeSet的介绍和使用"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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

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

NavigableSet简介
public interface NavigableSet extends SortedSet {    /** 返回小于给定元素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 subSet(E fromElement, boolean fromInclusive,                           E toElement,   boolean toInclusive);    /** 返回toElement之前的元素 可设置是否包含toElement元素 **/    NavigableSet headSet(E toElement, boolean inclusive);    /** 返回fromElement之后的元素 可设置包含fromElement元素 **/    NavigableSet tailSet(E fromElement, boolean inclusive);    /** 返回fromElement和toElement之间的元素 包含fromElement但不包含toElement **/    SortedSet subSet(E fromElement, E toElement);    /** 返回toElement之前的元素 不包含toElement元素 **/    SortedSet headSet(E toElement);    /** 返回fromElement之后的元素 包含fromElement元素 **/    SortedSet tailSet(E fromElement);}

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

SortedSet简介
public interface SortedSet extends Set {    /** 返回当前集合元素排序的比较器 **/    Comparator comparator();    /** 返回fromElement和toElement之间的元素 包含fromElement但不包含toElement **/    SortedSet subSet(E fromElement, E toElement);    /** 返回toElement之前的元素 不包含toElement元素 **/    SortedSet headSet(E toElement);    /** 返回fromElement之后的元素 包含fromElement元素 **/    SortedSet tailSet(E fromElement);    /** 返回第一个元素 **/    E first();    /** 返回最后一个元素 **/    E last();}
TreeSet成员变量
/** 具体存储元素的NavigableMap **/private transient NavigableMap m;private static final Object PRESENT = new Object();
TreeSet构造函数
MyTreeSet(NavigableMap m) {    this.m = m;}/** 默认构造函数  基于TreeMap实现 **/public MyTreeSet() {    this(new TreeMap<>());}/** 提供排序方法的构造函数 **/public MyTreeSet(Comparator comparator) {    this(new TreeMap<>(comparator));}/** 根据给定集合构造函数 **/public MyTreeSet(Collection c) {    this();    addAll(c);}/** 根据给定排序的集合构造函数 **/public MyTreeSet(SortedSet s) {    this(s.comparator());    addAll(s);}
元素添加
/** 添加单个元素 **/public boolean add(E e) {    return m.put(e, PRESENT)==null;}/** 添加集合元素 **/public boolean addAll(Collection c) {    if (m.size()==0 && c.size() > 0 &&            c instanceof SortedSet &&            m instanceof TreeMap) {        SortedSet set = (SortedSet) c;        TreeMap map = (TreeMap) m;        Comparator cc = set.comparator();        Comparator 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 comparator() {    return m.comparator();}public SortedSet subSet(E fromElement, E toElement) {    return subSet(fromElement, true, toElement, false);}public SortedSet headSet(E toElement) {    return headSet(toElement, false);}public SortedSet 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 = m.pollFirstEntry();    return e == null ? null : e.getKey();}public E pollLast() {    Map.Entry e = m.pollLastEntry();    return e == null ? null : e.getKey();}public Iterator iterator() {    return m.navigableKeySet().iterator();}public NavigableSet descendingSet() {    return m.descendingKeySet();}public Iterator descendingIterator() {    return m.navigableKeySet().descendingIterator();}public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {    return new TreeSet<>(m.subMap(fromElement, fromInclusive, toElement, toInclusive));}public NavigableSet headSet(E toElement, boolean inclusive) {    return new TreeSet<>(m.headMap(toElement, inclusive));}    public NavigableSet tailSet(E fromElement, boolean inclusive) {    return new TreeSet<>(m.tailMap(fromElement, inclusive));}

到此,关于"TreeSet的介绍和使用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0