jaroslav@1258: /*
jaroslav@1258: * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@1258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258: *
jaroslav@1258: * This code is free software; you can redistribute it and/or modify it
jaroslav@1258: * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258: * published by the Free Software Foundation. Oracle designates this
jaroslav@1258: * particular file as subject to the "Classpath" exception as provided
jaroslav@1258: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258: *
jaroslav@1258: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@1258: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258: * accompanied this code).
jaroslav@1258: *
jaroslav@1258: * You should have received a copy of the GNU General Public License version
jaroslav@1258: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258: *
jaroslav@1258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258: * or visit www.oracle.com if you need additional information or have any
jaroslav@1258: * questions.
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258: package java.util;
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * A {@link NavigableSet} implementation based on a {@link TreeMap}.
jaroslav@1258: * The elements are ordered using their {@linkplain Comparable natural
jaroslav@1258: * ordering}, or by a {@link Comparator} provided at set creation
jaroslav@1258: * time, depending on which constructor is used.
jaroslav@1258: *
jaroslav@1258: *
This implementation provides guaranteed log(n) time cost for the basic
jaroslav@1258: * operations ({@code add}, {@code remove} and {@code contains}).
jaroslav@1258: *
jaroslav@1258: *
Note that the ordering maintained by a set (whether or not an explicit
jaroslav@1258: * comparator is provided) must be consistent with equals if it is to
jaroslav@1258: * correctly implement the {@code Set} interface. (See {@code Comparable}
jaroslav@1258: * or {@code Comparator} for a precise definition of consistent with
jaroslav@1258: * equals.) This is so because the {@code Set} interface is defined in
jaroslav@1258: * terms of the {@code equals} operation, but a {@code TreeSet} instance
jaroslav@1258: * performs all element comparisons using its {@code compareTo} (or
jaroslav@1258: * {@code compare}) method, so two elements that are deemed equal by this method
jaroslav@1258: * are, from the standpoint of the set, equal. The behavior of a set
jaroslav@1258: * is well-defined even if its ordering is inconsistent with equals; it
jaroslav@1258: * just fails to obey the general contract of the {@code Set} interface.
jaroslav@1258: *
jaroslav@1258: *
Note that this implementation is not synchronized.
jaroslav@1258: * If multiple threads access a tree set concurrently, and at least one
jaroslav@1258: * of the threads modifies the set, it must be synchronized
jaroslav@1258: * externally. This is typically accomplished by synchronizing on some
jaroslav@1258: * object that naturally encapsulates the set.
jaroslav@1258: * If no such object exists, the set should be "wrapped" using the
jaroslav@1258: * {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}
jaroslav@1258: * method. This is best done at creation time, to prevent accidental
jaroslav@1258: * unsynchronized access to the set:
jaroslav@1258: * SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
jaroslav@1258: *
jaroslav@1258: * The iterators returned by this class's {@code iterator} method are
jaroslav@1258: * fail-fast: if the set is modified at any time after the iterator is
jaroslav@1258: * created, in any way except through the iterator's own {@code remove}
jaroslav@1258: * method, the iterator will throw a {@link ConcurrentModificationException}.
jaroslav@1258: * Thus, in the face of concurrent modification, the iterator fails quickly
jaroslav@1258: * and cleanly, rather than risking arbitrary, non-deterministic behavior at
jaroslav@1258: * an undetermined time in the future.
jaroslav@1258: *
jaroslav@1258: *
Note that the fail-fast behavior of an iterator cannot be guaranteed
jaroslav@1258: * as it is, generally speaking, impossible to make any hard guarantees in the
jaroslav@1258: * presence of unsynchronized concurrent modification. Fail-fast iterators
jaroslav@1258: * throw {@code ConcurrentModificationException} on a best-effort basis.
jaroslav@1258: * Therefore, it would be wrong to write a program that depended on this
jaroslav@1258: * exception for its correctness: the fail-fast behavior of iterators
jaroslav@1258: * should be used only to detect bugs.
jaroslav@1258: *
jaroslav@1258: *
This class is a member of the
jaroslav@1258: *
jaroslav@1258: * Java Collections Framework.
jaroslav@1258: *
jaroslav@1258: * @param the type of elements maintained by this set
jaroslav@1258: *
jaroslav@1258: * @author Josh Bloch
jaroslav@1258: * @see Collection
jaroslav@1258: * @see Set
jaroslav@1258: * @see HashSet
jaroslav@1258: * @see Comparable
jaroslav@1258: * @see Comparator
jaroslav@1258: * @see TreeMap
jaroslav@1258: * @since 1.2
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258: public class TreeSet extends AbstractSet
jaroslav@1258: implements NavigableSet, Cloneable, java.io.Serializable
jaroslav@1258: {
jaroslav@1258: /**
jaroslav@1258: * The backing map.
jaroslav@1258: */
jaroslav@1258: private transient NavigableMap m;
jaroslav@1258:
jaroslav@1258: // Dummy value to associate with an Object in the backing Map
jaroslav@1258: private static final Object PRESENT = new Object();
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Constructs a set backed by the specified navigable map.
jaroslav@1258: */
jaroslav@1258: TreeSet(NavigableMap m) {
jaroslav@1258: this.m = m;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Constructs a new, empty tree set, sorted according to the
jaroslav@1258: * natural ordering of its elements. All elements inserted into
jaroslav@1258: * the set must implement the {@link Comparable} interface.
jaroslav@1258: * Furthermore, all such elements must be mutually
jaroslav@1258: * comparable: {@code e1.compareTo(e2)} must not throw a
jaroslav@1258: * {@code ClassCastException} for any elements {@code e1} and
jaroslav@1258: * {@code e2} in the set. If the user attempts to add an element
jaroslav@1258: * to the set that violates this constraint (for example, the user
jaroslav@1258: * attempts to add a string element to a set whose elements are
jaroslav@1258: * integers), the {@code add} call will throw a
jaroslav@1258: * {@code ClassCastException}.
jaroslav@1258: */
jaroslav@1258: public TreeSet() {
jaroslav@1258: this(new TreeMap());
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Constructs a new, empty tree set, sorted according to the specified
jaroslav@1258: * comparator. All elements inserted into the set must be mutually
jaroslav@1258: * comparable by the specified comparator: {@code comparator.compare(e1,
jaroslav@1258: * e2)} must not throw a {@code ClassCastException} for any elements
jaroslav@1258: * {@code e1} and {@code e2} in the set. If the user attempts to add
jaroslav@1258: * an element to the set that violates this constraint, the
jaroslav@1258: * {@code add} call will throw a {@code ClassCastException}.
jaroslav@1258: *
jaroslav@1258: * @param comparator the comparator that will be used to order this set.
jaroslav@1258: * If {@code null}, the {@linkplain Comparable natural
jaroslav@1258: * ordering} of the elements will be used.
jaroslav@1258: */
jaroslav@1258: public TreeSet(Comparator super E> comparator) {
jaroslav@1258: this(new TreeMap<>(comparator));
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Constructs a new tree set containing the elements in the specified
jaroslav@1258: * collection, sorted according to the natural ordering of its
jaroslav@1258: * elements. All elements inserted into the set must implement the
jaroslav@1258: * {@link Comparable} interface. Furthermore, all such elements must be
jaroslav@1258: * mutually comparable: {@code e1.compareTo(e2)} must not throw a
jaroslav@1258: * {@code ClassCastException} for any elements {@code e1} and
jaroslav@1258: * {@code e2} in the set.
jaroslav@1258: *
jaroslav@1258: * @param c collection whose elements will comprise the new set
jaroslav@1258: * @throws ClassCastException if the elements in {@code c} are
jaroslav@1258: * not {@link Comparable}, or are not mutually comparable
jaroslav@1258: * @throws NullPointerException if the specified collection is null
jaroslav@1258: */
jaroslav@1258: public TreeSet(Collection extends E> c) {
jaroslav@1258: this();
jaroslav@1258: addAll(c);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Constructs a new tree set containing the same elements and
jaroslav@1258: * using the same ordering as the specified sorted set.
jaroslav@1258: *
jaroslav@1258: * @param s sorted set whose elements will comprise the new set
jaroslav@1258: * @throws NullPointerException if the specified sorted set is null
jaroslav@1258: */
jaroslav@1258: public TreeSet(SortedSet s) {
jaroslav@1258: this(s.comparator());
jaroslav@1258: addAll(s);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Returns an iterator over the elements in this set in ascending order.
jaroslav@1258: *
jaroslav@1258: * @return an iterator over the elements in this set in ascending order
jaroslav@1258: */
jaroslav@1258: public Iterator iterator() {
jaroslav@1258: return m.navigableKeySet().iterator();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Returns an iterator over the elements in this set in descending order.
jaroslav@1258: *
jaroslav@1258: * @return an iterator over the elements in this set in descending order
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public Iterator descendingIterator() {
jaroslav@1258: return m.descendingKeySet().iterator();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public NavigableSet descendingSet() {
jaroslav@1258: return new TreeSet<>(m.descendingMap());
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Returns the number of elements in this set (its cardinality).
jaroslav@1258: *
jaroslav@1258: * @return the number of elements in this set (its cardinality)
jaroslav@1258: */
jaroslav@1258: public int size() {
jaroslav@1258: return m.size();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Returns {@code true} if this set contains no elements.
jaroslav@1258: *
jaroslav@1258: * @return {@code true} if this set contains no elements
jaroslav@1258: */
jaroslav@1258: public boolean isEmpty() {
jaroslav@1258: return m.isEmpty();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Returns {@code true} if this set contains the specified element.
jaroslav@1258: * More formally, returns {@code true} if and only if this set
jaroslav@1258: * contains an element {@code e} such that
jaroslav@1258: * (o==null ? e==null : o.equals(e)).
jaroslav@1258: *
jaroslav@1258: * @param o object to be checked for containment in this set
jaroslav@1258: * @return {@code true} if this set contains the specified element
jaroslav@1258: * @throws ClassCastException if the specified object cannot be compared
jaroslav@1258: * with the elements currently in the set
jaroslav@1258: * @throws NullPointerException if the specified element is null
jaroslav@1258: * and this set uses natural ordering, or its comparator
jaroslav@1258: * does not permit null elements
jaroslav@1258: */
jaroslav@1258: public boolean contains(Object o) {
jaroslav@1258: return m.containsKey(o);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Adds the specified element to this set if it is not already present.
jaroslav@1258: * More formally, adds the specified element {@code e} to this set if
jaroslav@1258: * the set contains no element {@code e2} such that
jaroslav@1258: * (e==null ? e2==null : e.equals(e2)).
jaroslav@1258: * If this set already contains the element, the call leaves the set
jaroslav@1258: * unchanged and returns {@code false}.
jaroslav@1258: *
jaroslav@1258: * @param e element to be added to this set
jaroslav@1258: * @return {@code true} if this set did not already contain the specified
jaroslav@1258: * element
jaroslav@1258: * @throws ClassCastException if the specified object cannot be compared
jaroslav@1258: * with the elements currently in this set
jaroslav@1258: * @throws NullPointerException if the specified element is null
jaroslav@1258: * and this set uses natural ordering, or its comparator
jaroslav@1258: * does not permit null elements
jaroslav@1258: */
jaroslav@1258: public boolean add(E e) {
jaroslav@1258: return m.put(e, PRESENT)==null;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Removes the specified element from this set if it is present.
jaroslav@1258: * More formally, removes an element {@code e} such that
jaroslav@1258: * (o==null ? e==null : o.equals(e)),
jaroslav@1258: * if this set contains such an element. Returns {@code true} if
jaroslav@1258: * this set contained the element (or equivalently, if this set
jaroslav@1258: * changed as a result of the call). (This set will not contain the
jaroslav@1258: * element once the call returns.)
jaroslav@1258: *
jaroslav@1258: * @param o object to be removed from this set, if present
jaroslav@1258: * @return {@code true} if this set contained the specified element
jaroslav@1258: * @throws ClassCastException if the specified object cannot be compared
jaroslav@1258: * with the elements currently in this set
jaroslav@1258: * @throws NullPointerException if the specified element is null
jaroslav@1258: * and this set uses natural ordering, or its comparator
jaroslav@1258: * does not permit null elements
jaroslav@1258: */
jaroslav@1258: public boolean remove(Object o) {
jaroslav@1258: return m.remove(o)==PRESENT;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Removes all of the elements from this set.
jaroslav@1258: * The set will be empty after this call returns.
jaroslav@1258: */
jaroslav@1258: public void clear() {
jaroslav@1258: m.clear();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Adds all of the elements in the specified collection to this set.
jaroslav@1258: *
jaroslav@1258: * @param c collection containing elements to be added to this set
jaroslav@1258: * @return {@code true} if this set changed as a result of the call
jaroslav@1258: * @throws ClassCastException if the elements provided cannot be compared
jaroslav@1258: * with the elements currently in the set
jaroslav@1258: * @throws NullPointerException if the specified collection is null or
jaroslav@1258: * if any element is null and this set uses natural ordering, or
jaroslav@1258: * its comparator does not permit null elements
jaroslav@1258: */
jaroslav@1258: public boolean addAll(Collection extends E> c) {
jaroslav@1258: // Use linear-time version if applicable
jaroslav@1258: if (m.size()==0 && c.size() > 0 &&
jaroslav@1258: c instanceof SortedSet &&
jaroslav@1258: m instanceof TreeMap) {
jaroslav@1258: SortedSet extends E> set = (SortedSet extends E>) c;
jaroslav@1258: TreeMap map = (TreeMap) m;
jaroslav@1258: Comparator super E> cc = (Comparator super E>) set.comparator();
jaroslav@1258: Comparator super E> mc = map.comparator();
jaroslav@1258: if (cc==mc || (cc != null && cc.equals(mc))) {
jaroslav@1258: map.addAllForTreeSet(set, PRESENT);
jaroslav@1258: return true;
jaroslav@1258: }
jaroslav@1258: }
jaroslav@1258: return super.addAll(c);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if {@code fromElement} or {@code toElement}
jaroslav@1258: * is null and this set uses natural ordering, or its comparator
jaroslav@1258: * does not permit null elements
jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public NavigableSet subSet(E fromElement, boolean fromInclusive,
jaroslav@1258: E toElement, boolean toInclusive) {
jaroslav@1258: return new TreeSet<>(m.subMap(fromElement, fromInclusive,
jaroslav@1258: toElement, toInclusive));
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if {@code toElement} is null and
jaroslav@1258: * this set uses natural ordering, or its comparator does
jaroslav@1258: * not permit null elements
jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public NavigableSet headSet(E toElement, boolean inclusive) {
jaroslav@1258: return new TreeSet<>(m.headMap(toElement, inclusive));
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if {@code fromElement} is null and
jaroslav@1258: * this set uses natural ordering, or its comparator does
jaroslav@1258: * not permit null elements
jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public NavigableSet tailSet(E fromElement, boolean inclusive) {
jaroslav@1258: return new TreeSet<>(m.tailMap(fromElement, inclusive));
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if {@code fromElement} or
jaroslav@1258: * {@code toElement} is null and this set uses natural ordering,
jaroslav@1258: * or its comparator does not permit null elements
jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258: */
jaroslav@1258: public SortedSet subSet(E fromElement, E toElement) {
jaroslav@1258: return subSet(fromElement, true, toElement, false);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if {@code toElement} is null
jaroslav@1258: * and this set uses natural ordering, or its comparator does
jaroslav@1258: * not permit null elements
jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258: */
jaroslav@1258: public SortedSet headSet(E toElement) {
jaroslav@1258: return headSet(toElement, false);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if {@code fromElement} is null
jaroslav@1258: * and this set uses natural ordering, or its comparator does
jaroslav@1258: * not permit null elements
jaroslav@1258: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1258: */
jaroslav@1258: public SortedSet tailSet(E fromElement) {
jaroslav@1258: return tailSet(fromElement, true);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: public Comparator super E> comparator() {
jaroslav@1258: return m.comparator();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws NoSuchElementException {@inheritDoc}
jaroslav@1258: */
jaroslav@1258: public E first() {
jaroslav@1258: return m.firstKey();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws NoSuchElementException {@inheritDoc}
jaroslav@1258: */
jaroslav@1258: public E last() {
jaroslav@1258: return m.lastKey();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: // NavigableSet API methods
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if the specified element is null
jaroslav@1258: * and this set uses natural ordering, or its comparator
jaroslav@1258: * does not permit null elements
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public E lower(E e) {
jaroslav@1258: return m.lowerKey(e);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if the specified element is null
jaroslav@1258: * and this set uses natural ordering, or its comparator
jaroslav@1258: * does not permit null elements
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public E floor(E e) {
jaroslav@1258: return m.floorKey(e);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if the specified element is null
jaroslav@1258: * and this set uses natural ordering, or its comparator
jaroslav@1258: * does not permit null elements
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public E ceiling(E e) {
jaroslav@1258: return m.ceilingKey(e);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @throws ClassCastException {@inheritDoc}
jaroslav@1258: * @throws NullPointerException if the specified element is null
jaroslav@1258: * and this set uses natural ordering, or its comparator
jaroslav@1258: * does not permit null elements
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public E higher(E e) {
jaroslav@1258: return m.higherKey(e);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public E pollFirst() {
jaroslav@1258: Map.Entry e = m.pollFirstEntry();
jaroslav@1258: return (e == null) ? null : e.getKey();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * @since 1.6
jaroslav@1258: */
jaroslav@1258: public E pollLast() {
jaroslav@1258: Map.Entry e = m.pollLastEntry();
jaroslav@1258: return (e == null) ? null : e.getKey();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Returns a shallow copy of this {@code TreeSet} instance. (The elements
jaroslav@1258: * themselves are not cloned.)
jaroslav@1258: *
jaroslav@1258: * @return a shallow copy of this set
jaroslav@1258: */
jaroslav@1258: public Object clone() {
jaroslav@1258: TreeSet clone = null;
jaroslav@1258: try {
jaroslav@1258: clone = (TreeSet) super.clone();
jaroslav@1258: } catch (CloneNotSupportedException e) {
jaroslav@1258: throw new InternalError();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: clone.m = new TreeMap<>(m);
jaroslav@1258: return clone;
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Save the state of the {@code TreeSet} instance to a stream (that is,
jaroslav@1258: * serialize it).
jaroslav@1258: *
jaroslav@1258: * @serialData Emits the comparator used to order this set, or
jaroslav@1258: * {@code null} if it obeys its elements' natural ordering
jaroslav@1258: * (Object), followed by the size of the set (the number of
jaroslav@1258: * elements it contains) (int), followed by all of its
jaroslav@1258: * elements (each an Object) in order (as determined by the
jaroslav@1258: * set's Comparator, or by the elements' natural ordering if
jaroslav@1258: * the set has no Comparator).
jaroslav@1258: */
jaroslav@1258: private void writeObject(java.io.ObjectOutputStream s)
jaroslav@1258: throws java.io.IOException {
jaroslav@1258: // Write out any hidden stuff
jaroslav@1258: s.defaultWriteObject();
jaroslav@1258:
jaroslav@1258: // Write out Comparator
jaroslav@1258: s.writeObject(m.comparator());
jaroslav@1258:
jaroslav@1258: // Write out size
jaroslav@1258: s.writeInt(m.size());
jaroslav@1258:
jaroslav@1258: // Write out all elements in the proper order.
jaroslav@1258: for (E e : m.keySet())
jaroslav@1258: s.writeObject(e);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Reconstitute the {@code TreeSet} instance from a stream (that is,
jaroslav@1258: * deserialize it).
jaroslav@1258: */
jaroslav@1258: private void readObject(java.io.ObjectInputStream s)
jaroslav@1258: throws java.io.IOException, ClassNotFoundException {
jaroslav@1258: // Read in any hidden stuff
jaroslav@1258: s.defaultReadObject();
jaroslav@1258:
jaroslav@1258: // Read in Comparator
jaroslav@1258: Comparator super E> c = (Comparator super E>) s.readObject();
jaroslav@1258:
jaroslav@1258: // Create backing TreeMap
jaroslav@1258: TreeMap tm;
jaroslav@1258: if (c==null)
jaroslav@1258: tm = new TreeMap<>();
jaroslav@1258: else
jaroslav@1258: tm = new TreeMap<>(c);
jaroslav@1258: m = tm;
jaroslav@1258:
jaroslav@1258: // Read in size
jaroslav@1258: int size = s.readInt();
jaroslav@1258:
jaroslav@1258: tm.readTreeSet(size, s, PRESENT);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: private static final long serialVersionUID = -2479143000061671589L;
jaroslav@1258: }