285 }
286
287 /**
288 * Adds all of the elements in the specified collection to this set.
289 *
290 * @param c collection containing elements to be added to this set
291 * @return {@code true} if this set changed as a result of the call
292 * @throws ClassCastException if the elements provided cannot be compared
293 * with the elements currently in the set
294 * @throws NullPointerException if the specified collection is null or
295 * if any element is null and this set uses natural ordering, or
296 * its comparator does not permit null elements
297 */
298 public boolean addAll(Collection<? extends E> c) {
299 // Use linear-time version if applicable
300 if (m.size()==0 && c.size() > 0 &&
301 c instanceof SortedSet &&
302 m instanceof TreeMap) {
303 SortedSet<? extends E> set = (SortedSet<? extends E>) c;
304 TreeMap<E,Object> map = (TreeMap<E, Object>) m;
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,
452 */
453 public E pollFirst() {
454 Map.Entry<E,?> e = m.pollFirstEntry();
455 return (e == null) ? null : e.getKey();
456 }
457
458 /**
459 * @since 1.6
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(e);
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
502 s.writeObject(m.comparator());
503
504 // Write out size
505 s.writeInt(m.size());
506
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 }
|
285 }
286
287 /**
288 * Adds all of the elements in the specified collection to this set.
289 *
290 * @param c collection containing elements to be added to this set
291 * @return {@code true} if this set changed as a result of the call
292 * @throws ClassCastException if the elements provided cannot be compared
293 * with the elements currently in the set
294 * @throws NullPointerException if the specified collection is null or
295 * if any element is null and this set uses natural ordering, or
296 * its comparator does not permit null elements
297 */
298 public boolean addAll(Collection<? extends E> c) {
299 // Use linear-time version if applicable
300 if (m.size()==0 && c.size() > 0 &&
301 c instanceof SortedSet &&
302 m instanceof TreeMap) {
303 SortedSet<? extends E> set = (SortedSet<? extends E>) c;
304 TreeMap<E,Object> map = (TreeMap<E, Object>) m;
305 Comparator<?> cc = 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,
452 */
453 public E pollFirst() {
454 Map.Entry<E,?> e = m.pollFirstEntry();
455 return (e == null) ? null : e.getKey();
456 }
457
458 /**
459 * @since 1.6
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 @SuppressWarnings("unchecked")
473 public Object clone() {
474 TreeSet<E> clone;
475 try {
476 clone = (TreeSet<E>) super.clone();
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
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 private static final long serialVersionUID = -2479143000061671589L;
537 }
|