src/java.base/share/classes/java/util/TreeSet.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57895
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 49433
diff changeset
     2
 * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A {@link NavigableSet} implementation based on a {@link TreeMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The elements are ordered using their {@linkplain Comparable natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * ordering}, or by a {@link Comparator} provided at set creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * time, depending on which constructor is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>This implementation provides guaranteed log(n) time cost for the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * operations ({@code add}, {@code remove} and {@code contains}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>Note that the ordering maintained by a set (whether or not an explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * comparator is provided) must be <i>consistent with equals</i> if it is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * correctly implement the {@code Set} interface.  (See {@code Comparable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * or {@code Comparator} for a precise definition of <i>consistent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * equals</i>.)  This is so because the {@code Set} interface is defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * terms of the {@code equals} operation, but a {@code TreeSet} instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * performs all element comparisons using its {@code compareTo} (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * {@code compare}) method, so two elements that are deemed equal by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * are, from the standpoint of the set, equal.  The behavior of a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <i>is</i> well-defined even if its ordering is inconsistent with equals; it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * just fails to obey the general contract of the {@code Set} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * If multiple threads access a tree set concurrently, and at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * of the threads modifies the set, it <i>must</i> be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * externally.  This is typically accomplished by synchronizing on some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * object that naturally encapsulates the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * If no such object exists, the set should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * unsynchronized access to the set: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>The iterators returned by this class's {@code iterator} method are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <i>fail-fast</i>: if the set is modified at any time after the iterator is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * created, in any way except through the iterator's own {@code remove}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * method, the iterator will throw a {@link ConcurrentModificationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Thus, in the face of concurrent modification, the iterator fails quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * and cleanly, rather than risking arbitrary, non-deterministic behavior at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * throw {@code ConcurrentModificationException} on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * exception for its correctness:   <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 47216
diff changeset
    77
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @param <E> the type of elements maintained by this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see     Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see     HashSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @see     Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @see     Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class TreeSet<E> extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    implements NavigableSet<E>, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private transient NavigableMap<E,Object> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // Dummy value to associate with an Object in the backing Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final Object PRESENT = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Constructs a set backed by the specified navigable map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    TreeSet(NavigableMap<E,Object> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Constructs a new, empty tree set, sorted according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * natural ordering of its elements.  All elements inserted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the set must implement the {@link Comparable} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Furthermore, all such elements must be <i>mutually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * comparable</i>: {@code e1.compareTo(e2)} must not throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * {@code ClassCastException} for any elements {@code e1} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * {@code e2} in the set.  If the user attempts to add an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * to the set that violates this constraint (for example, the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * attempts to add a string element to a set whose elements are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * integers), the {@code add} call will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * {@code ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public TreeSet() {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19435
diff changeset
   124
        this(new TreeMap<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Constructs a new, empty tree set, sorted according to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * comparator.  All elements inserted into the set must be <i>mutually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * comparable</i> by the specified comparator: {@code comparator.compare(e1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * e2)} must not throw a {@code ClassCastException} for any elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * {@code e1} and {@code e2} in the set.  If the user attempts to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * an element to the set that violates this constraint, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * {@code add} call will throw a {@code ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param comparator the comparator that will be used to order this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *        If {@code null}, the {@linkplain Comparable natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *        ordering} of the elements will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public TreeSet(Comparator<? super E> comparator) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   141
        this(new TreeMap<>(comparator));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Constructs a new tree set containing the elements in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * collection, sorted according to the <i>natural ordering</i> of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * elements.  All elements inserted into the set must implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * {@link Comparable} interface.  Furthermore, all such elements must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * {@code ClassCastException} for any elements {@code e1} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * {@code e2} in the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param c collection whose elements will comprise the new set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws ClassCastException if the elements in {@code c} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         not {@link Comparable}, or are not mutually comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public TreeSet(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Constructs a new tree set containing the same elements and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * using the same ordering as the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param s sorted set whose elements will comprise the new set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @throws NullPointerException if the specified sorted set is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public TreeSet(SortedSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this(s.comparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        addAll(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns an iterator over the elements in this set in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return an iterator over the elements in this set in ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return m.navigableKeySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns an iterator over the elements in this set in descending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return an iterator over the elements in this set in descending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return m.descendingKeySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public NavigableSet<E> descendingSet() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   198
        return new TreeSet<>(m.descendingMap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Returns the number of elements in this set (its cardinality).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return the number of elements in this set (its cardinality)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Returns {@code true} if this set contains no elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return {@code true} if this set contains no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return m.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns {@code true} if this set contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * More formally, returns {@code true} if and only if this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * contains an element {@code e} such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   223
     * {@code Objects.equals(o, e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param o object to be checked for containment in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return {@code true} if this set contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @throws ClassCastException if the specified object cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *         with the elements currently in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return m.containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Adds the specified element to this set if it is not already present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * More formally, adds the specified element {@code e} to this set if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * the set contains no element {@code e2} such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   241
     * {@code Objects.equals(e, e2)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * If this set already contains the element, the call leaves the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * unchanged and returns {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param e element to be added to this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return {@code true} if this set did not already contain the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *         element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @throws ClassCastException if the specified object cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *         with the elements currently in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return m.put(e, PRESENT)==null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Removes the specified element from this set if it is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * More formally, removes an element {@code e} such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   261
     * {@code Objects.equals(o, e)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * if this set contains such an element.  Returns {@code true} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * this set contained the element (or equivalently, if this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * changed as a result of the call).  (This set will not contain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * element once the call returns.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param o object to be removed from this set, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return {@code true} if this set contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @throws ClassCastException if the specified object cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *         with the elements currently in this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return m.remove(o)==PRESENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Removes all of the elements from this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * The set will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        m.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Adds all of the elements in the specified collection to this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param c collection containing elements to be added to this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return {@code true} if this set changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @throws ClassCastException if the elements provided cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *         with the elements currently in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @throws NullPointerException if the specified collection is null or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *         if any element is null and this set uses natural ordering, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *         its comparator does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public  boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // Use linear-time version if applicable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (m.size()==0 && c.size() > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            c instanceof SortedSet &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            m instanceof TreeMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            SortedSet<? extends E> set = (SortedSet<? extends E>) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            TreeMap<E,Object> map = (TreeMap<E, Object>) m;
57895
82a71d82e326 8226831: Use Objects.equals() when appropriate
igerasim
parents: 49433
diff changeset
   305
            if (Objects.equals(set.comparator(), map.comparator())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                map.addAllForTreeSet(set, PRESENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return super.addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws NullPointerException if {@code fromElement} or {@code toElement}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *         is null and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                  E toElement,   boolean toInclusive) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   323
        return new TreeSet<>(m.subMap(fromElement, fromInclusive,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                       toElement,   toInclusive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @throws NullPointerException if {@code toElement} is null and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *         this set uses natural ordering, or its comparator does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *         not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public NavigableSet<E> headSet(E toElement, boolean inclusive) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   336
        return new TreeSet<>(m.headMap(toElement, inclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @throws NullPointerException if {@code fromElement} is null and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *         this set uses natural ordering, or its comparator does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *         not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   348
        return new TreeSet<>(m.tailMap(fromElement, inclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @throws NullPointerException if {@code fromElement} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *         {@code toElement} is null and this set uses natural ordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *         or its comparator does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public SortedSet<E> subSet(E fromElement, E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return subSet(fromElement, true, toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @throws NullPointerException if {@code toElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         and this set uses natural ordering, or its comparator does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *         not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public SortedSet<E> headSet(E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return headSet(toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @throws NullPointerException if {@code fromElement} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *         and this set uses natural ordering, or its comparator does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *         not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public SortedSet<E> tailSet(E fromElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return tailSet(fromElement, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public Comparator<? super E> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return m.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public E first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return m.firstKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public E last() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return m.lastKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    // NavigableSet API methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public E lower(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return m.lowerKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public E floor(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return m.floorKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public E ceiling(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return m.ceilingKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *         and this set uses natural ordering, or its comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *         does not permit null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public E higher(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return m.higherKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public E pollFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Map.Entry<E,?> e = m.pollFirstEntry();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   453
        return (e == null) ? null : e.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public E pollLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        Map.Entry<E,?> e = m.pollLastEntry();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   461
        return (e == null) ? null : e.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Returns a shallow copy of this {@code TreeSet} instance. (The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * themselves are not cloned.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @return a shallow copy of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   470
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public Object clone() {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   472
        TreeSet<E> clone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            clone = (TreeSet<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 7816
diff changeset
   476
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   479
        clone.m = new TreeMap<>(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Save the state of the {@code TreeSet} instance to a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @serialData Emits the comparator used to order this set, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *             {@code null} if it obeys its elements' natural ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *             (Object), followed by the size of the set (the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *             elements it contains) (int), followed by all of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *             elements (each an Object) in order (as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *             set's Comparator, or by the elements' natural ordering if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *             the set has no Comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57895
diff changeset
   495
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // Write out any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // Write out Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        s.writeObject(m.comparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        s.writeInt(m.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        // Write out all elements in the proper order.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   508
        for (E e : m.keySet())
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   509
            s.writeObject(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Reconstitute the {@code TreeSet} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57895
diff changeset
   516
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // Read in any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // Read in Comparator
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   523
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   524
            Comparator<? super E> c = (Comparator<? super E>) s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // Create backing TreeMap
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   527
        TreeMap<E,Object> tm = new TreeMap<>(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        m = tm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // Read in size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        tm.readTreeSet(size, s, PRESENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
19435
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   536
    /**
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   537
     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   538
     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   539
     * set.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   540
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   541
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   542
     * {@link Spliterator#DISTINCT}, {@link Spliterator#SORTED}, and
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   543
     * {@link Spliterator#ORDERED}.  Overriding implementations should document
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   544
     * the reporting of additional characteristic values.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   545
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   546
     * <p>The spliterator's comparator (see
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   547
     * {@link java.util.Spliterator#getComparator()}) is {@code null} if
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   548
     * the tree set's comparator (see {@link #comparator()}) is {@code null}.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   549
     * Otherwise, the spliterator's comparator is the same as or imposes the
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   550
     * same total ordering as the tree set's comparator.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   551
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   552
     * @return a {@code Spliterator} over the elements in this set
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   553
     * @since 1.8
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   554
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   555
    public Spliterator<E> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   556
        return TreeMap.keySpliteratorFor(m);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   557
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 14342
diff changeset
   558
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57895
diff changeset
   559
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    private static final long serialVersionUID = -2479143000061671589L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
}