
// 维护着一个导航Map private transient NavigableMap2、构造方法m; // 空对象,作为键值对的值 private static final Object PRESENT = new Object();
TreeSet(NavigableMap3、方法介绍m) { this.m = m; } public TreeSet() { this(new TreeMap ()); } public TreeSet(Comparator super E> comparator) { this(new TreeMap<>(comparator)); } public TreeSet(Collection extends E> c) { this(); addAll(c); } public TreeSet(SortedSet s) { this(s.comparator()); addAll(s); }
public Iteratoriterator() { return m.navigableKeySet().iterator(); } public Iterator descendingIterator() { return m.descendingKeySet().iterator(); } public NavigableSet descendingSet() { return new TreeSet<>(m.descendingMap()); }
这里提供了各种迭代器的实现。
public int size() {
return m.size();
}
public boolean isEmpty() {
return m.isEmpty();
}
返回集合大小
public boolean contains(Object o) {
return m.containsKey(o);
}
因为set集合只使用了TreeMap的键部分,所以contains方法调用TreeMap的containsKey方法。
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();
}
添加和移除方法。
public boolean addAll(Collection extends E> c) {
// Use linear-time version if applicable
if (m.size()==0 && c.size() > 0 &&
c instanceof SortedSet &&
m instanceof TreeMap) {
SortedSet extends E> set = (SortedSet extends E>) c;
TreeMap map = (TreeMap) 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 addAll(Collection extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
首先判断集合c和TreeMap 的comparator比较器是否相同,如果不相同就调用父类AbstractCollection的addAll方法。
public NavigableSetsubSet(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)); } 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 Comparator super E> comparator() {
return m.comparator();
}
获取比较器。
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 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();
}
调用TreeMap的方法。
它和TreeMap的关系就行HashSet与HashMap的关系。