这篇文章主要介绍“TreeSet的介绍和使用”,在日常操作中,相信很多人在TreeSet的介绍和使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”TreeSet的介绍和使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
public class MyTreeSet<E>
extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, Serializable {
}
TreeSet继承了AbstractSet抽象类且实现了NavigableSet接口,是一个有序的集合,基于TreeMap实现,支持自然排序或者提供的Comparator进行排序。
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
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();
}
/** 具体存储元素的NavigableMap **/
private transient NavigableMap<E,Object> m;
private static final Object PRESENT = new Object();
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();
}
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();
}
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));
}
到此,关于“TreeSet的介绍和使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/qgmzzmn/blog/3100935