Java Collection class analysis-Treeset, java set-treeset

Source: Internet
Author: User
Tags addall

Java Collection class analysis-Treeset, java set-treeset

// From the source code of the treeset below, we can see that it is basically a layer of packaging of treeMap. Therefore, there are too many introductions here. For details, we can see the specific implementation of treeMap.
// Implement the NavigableSet and set interfaces. Among them, NavigableSet implements the sortSet interface public class TreeSet <E> extends AbstractSet <E> implements NavigableSet <E>, Cloneable, and java. io. serializable {// here we can see that treeset and hashset are similar to private transient NavigableMap implemented based on map <E, Object> m; // construct a private static final Object PRESENT = new Object (); TreeSet (NavigableMap <E, Object> m) {this. m = m;} // you can see that it is actually treemap public TreeSet () {thi S (new TreeMap <E, Object> () ;}// a custom Comparator created in the Policy mode, but the public TreeSet (Comparator <? Super E> comparator) {this (new TreeMap <> (comparator);} public TreeSet (Collection <? Extends E> c) {this (); addAll (c);} public TreeSet (SortedSet <E> s) {this (s. comparator (); addAll (s) ;}// the return value Iterator returns the 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. siz E ();} public boolean isEmpty () {return m. isEmpty ();} // determines whether the public boolean contains (Object o) {return m. containsKey (o);} // add the public boolean add (E e) {return m. put (e, PRESENT) = null;} // Delete the public boolean remove (Object o) {return m. remove (o) = PRESENT;} // clear map 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 tosponsive) {return new TreeSet <> (m. subMap (fromElement, fromInclusive, toElement, toInclusive);} public NavigableSet <E> headSet (E toElement, boolean intrusive) {return new TreeSet <> (m. headMap (toElemen T, random SIVE);} public NavigableSet <E> tailSet (E fromElement, boolean random SIVE) {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 (fromEl Ement, true);} public Comparator <? Super E> comparator () {return m. comparator ();} public E first () {return m. firstKey ();} public E last () {return m. lastKey ();} // NavigableSet API methods 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) {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. writeObjec T (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 <? Super E> c = (Comparator <? Super E>) s. readObject (); // Create backing 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 ;}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.