您好,登錄后才能下訂單哦!
這篇文章主要介紹“TreeSet的介紹和使用”,在日常操作中,相信很多人在TreeSet的介紹和使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”TreeSet的介紹和使用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
public class MyTreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable { }
TreeSet繼承了AbstractSet抽象類且實(shí)現(xiàn)了NavigableSet接口,是一個有序的集合,基于TreeMap實(shí)現(xiàn),支持自然排序或者提供的Comparator進(jìn)行排序。
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之間的元素 可設(shè)置是否包含fromElement和toElement **/ NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive); /** 返回toElement之前的元素 可設(shè)置是否包含toElement元素 **/ NavigableSet<E> headSet(E toElement, boolean inclusive); /** 返回fromElement之后的元素 可設(shè)置包含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> { /** 返回當(dāng)前集合元素排序的比較器 **/ 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; } /** 默認(rèn)構(gòu)造函數(shù) 基于TreeMap實(shí)現(xiàn) **/ public MyTreeSet() { this(new TreeMap<>()); } /** 提供排序方法的構(gòu)造函數(shù) **/ public MyTreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } /** 根據(jù)給定集合構(gòu)造函數(shù) **/ public MyTreeSet(Collection<? extends E> c) { this(); addAll(c); } /** 根據(jù)給定排序的集合構(gòu)造函數(shù) **/ 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; } /** 移除當(dāng)前集合所有元素 **/ 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)); }
到此,關(guān)于“TreeSet的介紹和使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。