121 * {@code ClassCastException}.
122 */
123 public TreeSet() {
124 this(new TreeMap<E,Object>());
125 }
126
127 /**
128 * Constructs a new, empty tree set, sorted according to the specified
129 * comparator. All elements inserted into the set must be <i>mutually
130 * comparable</i> by the specified comparator: {@code comparator.compare(e1,
131 * e2)} must not throw a {@code ClassCastException} for any elements
132 * {@code e1} and {@code e2} in the set. If the user attempts to add
133 * an element to the set that violates this constraint, the
134 * {@code add} call will throw a {@code ClassCastException}.
135 *
136 * @param comparator the comparator that will be used to order this set.
137 * If {@code null}, the {@linkplain Comparable natural
138 * ordering} of the elements will be used.
139 */
140 public TreeSet(Comparator<? super E> comparator) {
141 this(new TreeMap<E,Object>(comparator));
142 }
143
144 /**
145 * Constructs a new tree set containing the elements in the specified
146 * collection, sorted according to the <i>natural ordering</i> of its
147 * elements. All elements inserted into the set must implement the
148 * {@link Comparable} interface. Furthermore, all such elements must be
149 * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
150 * {@code ClassCastException} for any elements {@code e1} and
151 * {@code e2} in the set.
152 *
153 * @param c collection whose elements will comprise the new set
154 * @throws ClassCastException if the elements in {@code c} are
155 * not {@link Comparable}, or are not mutually comparable
156 * @throws NullPointerException if the specified collection is null
157 */
158 public TreeSet(Collection<? extends E> c) {
159 this();
160 addAll(c);
161 }
178 * @return an iterator over the elements in this set in ascending order
179 */
180 public Iterator<E> iterator() {
181 return m.navigableKeySet().iterator();
182 }
183
184 /**
185 * Returns an iterator over the elements in this set in descending order.
186 *
187 * @return an iterator over the elements in this set in descending order
188 * @since 1.6
189 */
190 public Iterator<E> descendingIterator() {
191 return m.descendingKeySet().iterator();
192 }
193
194 /**
195 * @since 1.6
196 */
197 public NavigableSet<E> descendingSet() {
198 return new TreeSet<E>(m.descendingMap());
199 }
200
201 /**
202 * Returns the number of elements in this set (its cardinality).
203 *
204 * @return the number of elements in this set (its cardinality)
205 */
206 public int size() {
207 return m.size();
208 }
209
210 /**
211 * Returns {@code true} if this set contains no elements.
212 *
213 * @return {@code true} if this set contains no elements
214 */
215 public boolean isEmpty() {
216 return m.isEmpty();
217 }
218
305 Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
306 Comparator<? super E> mc = map.comparator();
307 if (cc==mc || (cc != null && cc.equals(mc))) {
308 map.addAllForTreeSet(set, PRESENT);
309 return true;
310 }
311 }
312 return super.addAll(c);
313 }
314
315 /**
316 * @throws ClassCastException {@inheritDoc}
317 * @throws NullPointerException if {@code fromElement} or {@code toElement}
318 * is null and this set uses natural ordering, or its comparator
319 * does not permit null elements
320 * @throws IllegalArgumentException {@inheritDoc}
321 * @since 1.6
322 */
323 public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
324 E toElement, boolean toInclusive) {
325 return new TreeSet<E>(m.subMap(fromElement, fromInclusive,
326 toElement, toInclusive));
327 }
328
329 /**
330 * @throws ClassCastException {@inheritDoc}
331 * @throws NullPointerException if {@code toElement} is null and
332 * this set uses natural ordering, or its comparator does
333 * not permit null elements
334 * @throws IllegalArgumentException {@inheritDoc}
335 * @since 1.6
336 */
337 public NavigableSet<E> headSet(E toElement, boolean inclusive) {
338 return new TreeSet<E>(m.headMap(toElement, inclusive));
339 }
340
341 /**
342 * @throws ClassCastException {@inheritDoc}
343 * @throws NullPointerException if {@code fromElement} is null and
344 * this set uses natural ordering, or its comparator does
345 * not permit null elements
346 * @throws IllegalArgumentException {@inheritDoc}
347 * @since 1.6
348 */
349 public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
350 return new TreeSet<E>(m.tailMap(fromElement, inclusive));
351 }
352
353 /**
354 * @throws ClassCastException {@inheritDoc}
355 * @throws NullPointerException if {@code fromElement} or
356 * {@code toElement} is null and this set uses natural ordering,
357 * or its comparator does not permit null elements
358 * @throws IllegalArgumentException {@inheritDoc}
359 */
360 public SortedSet<E> subSet(E fromElement, E toElement) {
361 return subSet(fromElement, true, toElement, false);
362 }
363
364 /**
365 * @throws ClassCastException {@inheritDoc}
366 * @throws NullPointerException if {@code toElement} is null
367 * and this set uses natural ordering, or its comparator does
368 * not permit null elements
369 * @throws IllegalArgumentException {@inheritDoc}
370 */
460 */
461 public E pollLast() {
462 Map.Entry<E,?> e = m.pollLastEntry();
463 return (e == null) ? null : e.getKey();
464 }
465
466 /**
467 * Returns a shallow copy of this {@code TreeSet} instance. (The elements
468 * themselves are not cloned.)
469 *
470 * @return a shallow copy of this set
471 */
472 public Object clone() {
473 TreeSet<E> clone = null;
474 try {
475 clone = (TreeSet<E>) super.clone();
476 } catch (CloneNotSupportedException e) {
477 throw new InternalError();
478 }
479
480 clone.m = new TreeMap<E,Object>(m);
481 return clone;
482 }
483
484 /**
485 * Save the state of the {@code TreeSet} instance to a stream (that is,
486 * serialize it).
487 *
488 * @serialData Emits the comparator used to order this set, or
489 * {@code null} if it obeys its elements' natural ordering
490 * (Object), followed by the size of the set (the number of
491 * elements it contains) (int), followed by all of its
492 * elements (each an Object) in order (as determined by the
493 * set's Comparator, or by the elements' natural ordering if
494 * the set has no Comparator).
495 */
496 private void writeObject(java.io.ObjectOutputStream s)
497 throws java.io.IOException {
498 // Write out any hidden stuff
499 s.defaultWriteObject();
500
507 // Write out all elements in the proper order.
508 for (E e : m.keySet())
509 s.writeObject(e);
510 }
511
512 /**
513 * Reconstitute the {@code TreeSet} instance from a stream (that is,
514 * deserialize it).
515 */
516 private void readObject(java.io.ObjectInputStream s)
517 throws java.io.IOException, ClassNotFoundException {
518 // Read in any hidden stuff
519 s.defaultReadObject();
520
521 // Read in Comparator
522 Comparator<? super E> c = (Comparator<? super E>) s.readObject();
523
524 // Create backing TreeMap
525 TreeMap<E,Object> tm;
526 if (c==null)
527 tm = new TreeMap<E,Object>();
528 else
529 tm = new TreeMap<E,Object>(c);
530 m = tm;
531
532 // Read in size
533 int size = s.readInt();
534
535 tm.readTreeSet(size, s, PRESENT);
536 }
537
538 private static final long serialVersionUID = -2479143000061671589L;
539 }
|
121 * {@code ClassCastException}.
122 */
123 public TreeSet() {
124 this(new TreeMap<E,Object>());
125 }
126
127 /**
128 * Constructs a new, empty tree set, sorted according to the specified
129 * comparator. All elements inserted into the set must be <i>mutually
130 * comparable</i> by the specified comparator: {@code comparator.compare(e1,
131 * e2)} must not throw a {@code ClassCastException} for any elements
132 * {@code e1} and {@code e2} in the set. If the user attempts to add
133 * an element to the set that violates this constraint, the
134 * {@code add} call will throw a {@code ClassCastException}.
135 *
136 * @param comparator the comparator that will be used to order this set.
137 * If {@code null}, the {@linkplain Comparable natural
138 * ordering} of the elements will be used.
139 */
140 public TreeSet(Comparator<? super E> comparator) {
141 this(new TreeMap<>(comparator));
142 }
143
144 /**
145 * Constructs a new tree set containing the elements in the specified
146 * collection, sorted according to the <i>natural ordering</i> of its
147 * elements. All elements inserted into the set must implement the
148 * {@link Comparable} interface. Furthermore, all such elements must be
149 * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
150 * {@code ClassCastException} for any elements {@code e1} and
151 * {@code e2} in the set.
152 *
153 * @param c collection whose elements will comprise the new set
154 * @throws ClassCastException if the elements in {@code c} are
155 * not {@link Comparable}, or are not mutually comparable
156 * @throws NullPointerException if the specified collection is null
157 */
158 public TreeSet(Collection<? extends E> c) {
159 this();
160 addAll(c);
161 }
178 * @return an iterator over the elements in this set in ascending order
179 */
180 public Iterator<E> iterator() {
181 return m.navigableKeySet().iterator();
182 }
183
184 /**
185 * Returns an iterator over the elements in this set in descending order.
186 *
187 * @return an iterator over the elements in this set in descending order
188 * @since 1.6
189 */
190 public Iterator<E> descendingIterator() {
191 return m.descendingKeySet().iterator();
192 }
193
194 /**
195 * @since 1.6
196 */
197 public NavigableSet<E> descendingSet() {
198 return new TreeSet<>(m.descendingMap());
199 }
200
201 /**
202 * Returns the number of elements in this set (its cardinality).
203 *
204 * @return the number of elements in this set (its cardinality)
205 */
206 public int size() {
207 return m.size();
208 }
209
210 /**
211 * Returns {@code true} if this set contains no elements.
212 *
213 * @return {@code true} if this set contains no elements
214 */
215 public boolean isEmpty() {
216 return m.isEmpty();
217 }
218
305 Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
306 Comparator<? super E> mc = map.comparator();
307 if (cc==mc || (cc != null && cc.equals(mc))) {
308 map.addAllForTreeSet(set, PRESENT);
309 return true;
310 }
311 }
312 return super.addAll(c);
313 }
314
315 /**
316 * @throws ClassCastException {@inheritDoc}
317 * @throws NullPointerException if {@code fromElement} or {@code toElement}
318 * is null and this set uses natural ordering, or its comparator
319 * does not permit null elements
320 * @throws IllegalArgumentException {@inheritDoc}
321 * @since 1.6
322 */
323 public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
324 E toElement, boolean toInclusive) {
325 return new TreeSet<>(m.subMap(fromElement, fromInclusive,
326 toElement, toInclusive));
327 }
328
329 /**
330 * @throws ClassCastException {@inheritDoc}
331 * @throws NullPointerException if {@code toElement} is null and
332 * this set uses natural ordering, or its comparator does
333 * not permit null elements
334 * @throws IllegalArgumentException {@inheritDoc}
335 * @since 1.6
336 */
337 public NavigableSet<E> headSet(E toElement, boolean inclusive) {
338 return new TreeSet<>(m.headMap(toElement, inclusive));
339 }
340
341 /**
342 * @throws ClassCastException {@inheritDoc}
343 * @throws NullPointerException if {@code fromElement} is null and
344 * this set uses natural ordering, or its comparator does
345 * not permit null elements
346 * @throws IllegalArgumentException {@inheritDoc}
347 * @since 1.6
348 */
349 public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
350 return new TreeSet<>(m.tailMap(fromElement, inclusive));
351 }
352
353 /**
354 * @throws ClassCastException {@inheritDoc}
355 * @throws NullPointerException if {@code fromElement} or
356 * {@code toElement} is null and this set uses natural ordering,
357 * or its comparator does not permit null elements
358 * @throws IllegalArgumentException {@inheritDoc}
359 */
360 public SortedSet<E> subSet(E fromElement, E toElement) {
361 return subSet(fromElement, true, toElement, false);
362 }
363
364 /**
365 * @throws ClassCastException {@inheritDoc}
366 * @throws NullPointerException if {@code toElement} is null
367 * and this set uses natural ordering, or its comparator does
368 * not permit null elements
369 * @throws IllegalArgumentException {@inheritDoc}
370 */
460 */
461 public E pollLast() {
462 Map.Entry<E,?> e = m.pollLastEntry();
463 return (e == null) ? null : e.getKey();
464 }
465
466 /**
467 * Returns a shallow copy of this {@code TreeSet} instance. (The elements
468 * themselves are not cloned.)
469 *
470 * @return a shallow copy of this set
471 */
472 public Object clone() {
473 TreeSet<E> clone = null;
474 try {
475 clone = (TreeSet<E>) super.clone();
476 } catch (CloneNotSupportedException e) {
477 throw new InternalError();
478 }
479
480 clone.m = new TreeMap<>(m);
481 return clone;
482 }
483
484 /**
485 * Save the state of the {@code TreeSet} instance to a stream (that is,
486 * serialize it).
487 *
488 * @serialData Emits the comparator used to order this set, or
489 * {@code null} if it obeys its elements' natural ordering
490 * (Object), followed by the size of the set (the number of
491 * elements it contains) (int), followed by all of its
492 * elements (each an Object) in order (as determined by the
493 * set's Comparator, or by the elements' natural ordering if
494 * the set has no Comparator).
495 */
496 private void writeObject(java.io.ObjectOutputStream s)
497 throws java.io.IOException {
498 // Write out any hidden stuff
499 s.defaultWriteObject();
500
507 // Write out all elements in the proper order.
508 for (E e : m.keySet())
509 s.writeObject(e);
510 }
511
512 /**
513 * Reconstitute the {@code TreeSet} instance from a stream (that is,
514 * deserialize it).
515 */
516 private void readObject(java.io.ObjectInputStream s)
517 throws java.io.IOException, ClassNotFoundException {
518 // Read in any hidden stuff
519 s.defaultReadObject();
520
521 // Read in Comparator
522 Comparator<? super E> c = (Comparator<? super E>) s.readObject();
523
524 // Create backing TreeMap
525 TreeMap<E,Object> tm;
526 if (c==null)
527 tm = new TreeMap<>();
528 else
529 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 private static final long serialVersionUID = -2479143000061671589L;
539 }
|