首页 > 学院 > 开发设计 > 正文

JDK之TreeSet源码解析

2019-11-06 08:24:40
字体:
来源:转载
供稿:网友

刚入java一年的萌新,对于简单的使用已毫不满足,最终为了一探究竟,翻开了JDK的源码,以下观点为自己的理解及看了多篇博客的总结,欢迎各位大神指出不对的地方,当然也欢迎和我一样刚学的同学,一起加油努力吧~~

TreeSet源码解析

刚写完TreeMap,准备下手看看TreeSet是如何实现的,打开源码后,突然就没有了写这篇的冲动,然而想想为了补齐集合类的分析,还是写了这篇,相信之前看过源码的同学应该知道HashMap与HashSet是如何实现的,可以说懂了HashMap就懂了HashSet,毕竟HashSet是在HashMap的基础上来实现的,TreeSet也一样,是基于HashMap来进行实现的,下面贴出代码,大家可以看看,就不做过多的解释了

public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, java.io.Serializable{ /** * 一个用于返回的map */ PRivate transient NavigableMap<E,Object> m; // 关联map的 private static final Object PRESENT = new Object(); /** * 无参构造 */ TreeSet(NavigableMap<E,Object> m) { this.m = m; } /** * 基于TreeMap的构造 */ public TreeSet() { this(new TreeMap<E,Object>()); } /** * 基于TreeMap的构造 */ public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } /** * 基于TreeMap的构造 */ public TreeSet(Collection<? extends E> c) { this(); addAll(c); } /** * 基于TreeMap的构造 */ public TreeSet(SortedSet<E> s) { this(s.comparator()); addAll(s); } /** * 迭代方法 */ public Iterator<E> iterator() { return m.navigableKeySet().iterator(); } /** * 迭代方法 */ public Iterator<E> descendingIterator() { return m.descendingKeySet().iterator(); } public NavigableSet<E> 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); } 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<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); } 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)); } 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 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 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 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; } private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out any hidden stuff s.defaultWriteObject(); // Write out Comparator s.writeObject(m.comparator()); // Write out size s.writeInt(m.size()); // Write out all elements in the proper order. for (E e : m.keySet()) s.writeObject(e); } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in any hidden stuff s.defaultReadObject(); // Read in Comparator Comparator<? super E> c = (Comparator<? super E>) s.readObject(); // Create backing TreeMap TreeMap<E,Object> tm; if (c==null) tm = new TreeMap<>(); else tm = new TreeMap<>(c); m = tm; // Read in size int size = s.readInt(); tm.readTreeSet(size, s, PRESENT); } private static final long serialVersionUID = -2479143000061671589L;}

这里只是列出来,大致看下,没做过多解释,毕竟都是基于TreeMap来实现的,如果不懂的可以先了解下TreeMapTreeMap源码解析,看完TreeMap的这篇可以说TreeSet也就会了。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表