1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| 1、add:将指定的元素添加到此 set(如果该元素尚未存在于 set 中)。 public boolean add(E e) { return m.put(e, PRESENT)==null; } 2、addAll:将指定 collection 中的所有元素添加到此 set 中。 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<? super E> cc = (Comparator<? super E>) 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); } 3、ceiling:返回此 set 中大于等于给定元素的最小元素;如果不存在这样的元素,则返回 null。 public E ceiling(E e) { return m.ceilingKey(e); } 4、clear:移除此 set 中的所有元素。 public void clear() { m.clear(); } 5、clone:返回 TreeSet 实例的浅表副本。属于浅拷贝。 public Object clone() { TreeSet<E> clone = null; try { clone = (TreeSet<E>) super.clone(); } catch (CloneNotSupportedException e) { throw new InternalError(); }
clone.m = new TreeMap<>(m); return clone; } 6、comparator:返回对此 set 中的元素进行排序的比较器;如果此 set 使用其元素的自然顺序,则返回 null。 public Comparator<? super E> comparator() { return m.comparator(); } 7、contains:如果此 set 包含指定的元素,则返回 true。 public boolean contains(Object o) { return m.containsKey(o); } 8、descendingIterator:返回在此 set 元素上按降序进行迭代的迭代器。 public Iterator<E> descendingIterator() { return m.descendingKeySet().iterator(); } 9、descendingSet:返回此 set 中所包含元素的逆序视图。 public NavigableSet<E> descendingSet() { return new TreeSet<>(m.descendingMap()); } 10、first:返回此 set 中当前第一个(最低)元素。 public E first() { return m.firstKey(); } 11、floor:返回此 set 中小于等于给定元素的最大元素;如果不存在这样的元素,则返回 null。 public E floor(E e) { return m.floorKey(e); } 12、headSet:返回此 set 的部分视图,其元素严格小于 toElement。 public SortedSet<E> headSet(E toElement) { return headSet(toElement, false); } 13、higher:返回此 set 中严格大于给定元素的最小元素;如果不存在这样的元素,则返回 null。 public E higher(E e) { return m.higherKey(e); } 14、isEmpty:如果此 set 不包含任何元素,则返回 true。 public boolean isEmpty() { return m.isEmpty(); } 15、iterator:返回在此 set 中的元素上按升序进行迭代的迭代器。 public Iterator<E> iterator() { return m.navigableKeySet().iterator(); } 16、last:返回此 set 中当前最后一个(最高)元素。 public E last() { return m.lastKey(); } 17、lower:返回此 set 中严格小于给定元素的最大元素;如果不存在这样的元素,则返回 null。 public E lower(E e) { return m.lowerKey(e); } 18、pollFirst:获取并移除第一个(最低)元素;如果此 set 为空,则返回 null。 public E pollFirst() { Map.Entry<E,?> e = m.pollFirstEntry(); return (e == null) ? null : e.getKey(); } 19、pollLast:获取并移除最后一个(最高)元素;如果此 set 为空,则返回 null。 public E pollLast() { Map.Entry<E,?> e = m.pollLastEntry(); return (e == null) ? null : e.getKey(); } 20、remove:将指定的元素从 set 中移除(如果该元素存在于此 set 中)。 public boolean remove(Object o) { return m.remove(o)==PRESENT; } 21、size:返回 set 中的元素数(set 的容量)。 public int size() { return m.size(); } 22、subSet:返回此 set 的部分视图
* 返回此 set 的部分视图,其元素范围从 fromElement 到 toElement。 */ public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { return new TreeSet<>(m.subMap(fromElement, fromInclusive, toElement, toInclusive)); } * 返回此 set 的部分视图,其元素从 fromElement(包括)到 toElement(不包括)。 */ public SortedSet<E> subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); } 23、tailSet:返回此 set 的部分视图
* 返回此 set 的部分视图,其元素大于(或等于,如果 inclusive 为 true)fromElement。 */ public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { return new TreeSet<>(m.tailMap(fromElement, inclusive)); } * 返回此 set 的部分视图,其元素大于等于 fromElement。 */ public SortedSet<E> tailSet(E fromElement) { return tailSet(fromElement, true); }
|