< prev index next >

src/java.base/share/classes/java/util/TreeSet.java

Print this page




 477         } catch (CloneNotSupportedException e) {
 478             throw new InternalError(e);
 479         }
 480 
 481         clone.m = new TreeMap<>(m);
 482         return clone;
 483     }
 484 
 485     /**
 486      * Save the state of the {@code TreeSet} instance to a stream (that is,
 487      * serialize it).
 488      *
 489      * @serialData Emits the comparator used to order this set, or
 490      *             {@code null} if it obeys its elements' natural ordering
 491      *             (Object), followed by the size of the set (the number of
 492      *             elements it contains) (int), followed by all of its
 493      *             elements (each an Object) in order (as determined by the
 494      *             set's Comparator, or by the elements' natural ordering if
 495      *             the set has no Comparator).
 496      */

 497     private void writeObject(java.io.ObjectOutputStream s)
 498         throws java.io.IOException {
 499         // Write out any hidden stuff
 500         s.defaultWriteObject();
 501 
 502         // Write out Comparator
 503         s.writeObject(m.comparator());
 504 
 505         // Write out size
 506         s.writeInt(m.size());
 507 
 508         // Write out all elements in the proper order.
 509         for (E e : m.keySet())
 510             s.writeObject(e);
 511     }
 512 
 513     /**
 514      * Reconstitute the {@code TreeSet} instance from a stream (that is,
 515      * deserialize it).
 516      */

 517     private void readObject(java.io.ObjectInputStream s)
 518         throws java.io.IOException, ClassNotFoundException {
 519         // Read in any hidden stuff
 520         s.defaultReadObject();
 521 
 522         // Read in Comparator
 523         @SuppressWarnings("unchecked")
 524             Comparator<? super E> c = (Comparator<? super E>) s.readObject();
 525 
 526         // Create backing TreeMap
 527         TreeMap<E,Object> tm = new TreeMap<>(c);
 528         m = tm;
 529 
 530         // Read in size
 531         int size = s.readInt();
 532 
 533         tm.readTreeSet(size, s, PRESENT);
 534     }
 535 
 536     /**


 539      * set.
 540      *
 541      * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
 542      * {@link Spliterator#DISTINCT}, {@link Spliterator#SORTED}, and
 543      * {@link Spliterator#ORDERED}.  Overriding implementations should document
 544      * the reporting of additional characteristic values.
 545      *
 546      * <p>The spliterator's comparator (see
 547      * {@link java.util.Spliterator#getComparator()}) is {@code null} if
 548      * the tree set's comparator (see {@link #comparator()}) is {@code null}.
 549      * Otherwise, the spliterator's comparator is the same as or imposes the
 550      * same total ordering as the tree set's comparator.
 551      *
 552      * @return a {@code Spliterator} over the elements in this set
 553      * @since 1.8
 554      */
 555     public Spliterator<E> spliterator() {
 556         return TreeMap.keySpliteratorFor(m);
 557     }
 558 

 559     private static final long serialVersionUID = -2479143000061671589L;
 560 }


 477         } catch (CloneNotSupportedException e) {
 478             throw new InternalError(e);
 479         }
 480 
 481         clone.m = new TreeMap<>(m);
 482         return clone;
 483     }
 484 
 485     /**
 486      * Save the state of the {@code TreeSet} instance to a stream (that is,
 487      * serialize it).
 488      *
 489      * @serialData Emits the comparator used to order this set, or
 490      *             {@code null} if it obeys its elements' natural ordering
 491      *             (Object), followed by the size of the set (the number of
 492      *             elements it contains) (int), followed by all of its
 493      *             elements (each an Object) in order (as determined by the
 494      *             set's Comparator, or by the elements' natural ordering if
 495      *             the set has no Comparator).
 496      */
 497     @java.io.Serial
 498     private void writeObject(java.io.ObjectOutputStream s)
 499         throws java.io.IOException {
 500         // Write out any hidden stuff
 501         s.defaultWriteObject();
 502 
 503         // Write out Comparator
 504         s.writeObject(m.comparator());
 505 
 506         // Write out size
 507         s.writeInt(m.size());
 508 
 509         // Write out all elements in the proper order.
 510         for (E e : m.keySet())
 511             s.writeObject(e);
 512     }
 513 
 514     /**
 515      * Reconstitute the {@code TreeSet} instance from a stream (that is,
 516      * deserialize it).
 517      */
 518     @java.io.Serial
 519     private void readObject(java.io.ObjectInputStream s)
 520         throws java.io.IOException, ClassNotFoundException {
 521         // Read in any hidden stuff
 522         s.defaultReadObject();
 523 
 524         // Read in Comparator
 525         @SuppressWarnings("unchecked")
 526             Comparator<? super E> c = (Comparator<? super E>) s.readObject();
 527 
 528         // Create backing TreeMap
 529         TreeMap<E,Object> tm = new TreeMap<>(c);
 530         m = tm;
 531 
 532         // Read in size
 533         int size = s.readInt();
 534 
 535         tm.readTreeSet(size, s, PRESENT);
 536     }
 537 
 538     /**


 541      * set.
 542      *
 543      * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
 544      * {@link Spliterator#DISTINCT}, {@link Spliterator#SORTED}, and
 545      * {@link Spliterator#ORDERED}.  Overriding implementations should document
 546      * the reporting of additional characteristic values.
 547      *
 548      * <p>The spliterator's comparator (see
 549      * {@link java.util.Spliterator#getComparator()}) is {@code null} if
 550      * the tree set's comparator (see {@link #comparator()}) is {@code null}.
 551      * Otherwise, the spliterator's comparator is the same as or imposes the
 552      * same total ordering as the tree set's comparator.
 553      *
 554      * @return a {@code Spliterator} over the elements in this set
 555      * @since 1.8
 556      */
 557     public Spliterator<E> spliterator() {
 558         return TreeMap.keySpliteratorFor(m);
 559     }
 560 
 561     @java.io.Serial
 562     private static final long serialVersionUID = -2479143000061671589L;
 563 }
< prev index next >