1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.UnaryOperator;
  29 
  30 /**
  31  * An ordered collection (also known as a <i>sequence</i>).  The user of this
  32  * interface has precise control over where in the list each element is
  33  * inserted.  The user can access elements by their integer index (position in
  34  * the list), and search for elements in the list.<p>
  35  *
  36  * Unlike sets, lists typically allow duplicate elements.  More formally,
  37  * lists typically allow pairs of elements {@code e1} and {@code e2}
  38  * such that {@code e1.equals(e2)}, and they typically allow multiple
  39  * null elements if they allow null elements at all.  It is not inconceivable
  40  * that someone might wish to implement a list that prohibits duplicates, by
  41  * throwing runtime exceptions when the user attempts to insert them, but we
  42  * expect this usage to be rare.<p>
  43  *
  44  * The {@code List} interface places additional stipulations, beyond those
  45  * specified in the {@code Collection} interface, on the contracts of the
  46  * {@code iterator}, {@code add}, {@code remove}, {@code equals}, and
  47  * {@code hashCode} methods.  Declarations for other inherited methods are
  48  * also included here for convenience.<p>
  49  *
  50  * The {@code List} interface provides four methods for positional (indexed)
  51  * access to list elements.  Lists (like Java arrays) are zero based.  Note
  52  * that these operations may execute in time proportional to the index value
  53  * for some implementations (the {@code LinkedList} class, for
  54  * example). Thus, iterating over the elements in a list is typically
  55  * preferable to indexing through it if the caller does not know the
  56  * implementation.<p>
  57  *
  58  * The {@code List} interface provides a special iterator, called a
  59  * {@code ListIterator}, that allows element insertion and replacement, and
  60  * bidirectional access in addition to the normal operations that the
  61  * {@code Iterator} interface provides.  A method is provided to obtain a
  62  * list iterator that starts at a specified position in the list.<p>
  63  *
  64  * The {@code List} interface provides two methods to search for a specified
  65  * object.  From a performance standpoint, these methods should be used with
  66  * caution.  In many implementations they will perform costly linear
  67  * searches.<p>
  68  *
  69  * The {@code List} interface provides two methods to efficiently insert and
  70  * remove multiple elements at an arbitrary point in the list.<p>
  71  *
  72  * Note: While it is permissible for lists to contain themselves as elements,
  73  * extreme caution is advised: the {@code equals} and {@code hashCode}
  74  * methods are no longer well defined on such a list.
  75  *
  76  * <p>Some list implementations have restrictions on the elements that
  77  * they may contain.  For example, some implementations prohibit null elements,
  78  * and some have restrictions on the types of their elements.  Attempting to
  79  * add an ineligible element throws an unchecked exception, typically
  80  * {@code NullPointerException} or {@code ClassCastException}.  Attempting
  81  * to query the presence of an ineligible element may throw an exception,
  82  * or it may simply return false; some implementations will exhibit the former
  83  * behavior and some will exhibit the latter.  More generally, attempting an
  84  * operation on an ineligible element whose completion would not result in
  85  * the insertion of an ineligible element into the list may throw an
  86  * exception or it may succeed, at the option of the implementation.
  87  * Such exceptions are marked as "optional" in the specification for this
  88  * interface.
  89  *
  90  * <h2><a id="unmodifiable">Unmodifiable Lists</a></h2>
  91  * <p>The {@link List#of(Object...) List.of} and
  92  * {@link List#copyOf List.copyOf} static factory methods
  93  * provide a convenient way to create unmodifiable lists. The {@code List}
  94  * instances created by these methods have the following characteristics:
  95  *
  96  * <ul>
  97  * <li>They are <a href="Collection.html#unmodifiable"><i>unmodifiable</i></a>. Elements cannot
  98  * be added, removed, or replaced. Calling any mutator method on the List
  99  * will always cause {@code UnsupportedOperationException} to be thrown.
 100  * However, if the contained elements are themselves mutable,
 101  * this may cause the List's contents to appear to change.
 102  * <li>They disallow {@code null} elements. Attempts to create them with
 103  * {@code null} elements result in {@code NullPointerException}.
 104  * <li>They are serializable if all elements are serializable.
 105  * <li>The order of elements in the list is the same as the order of the
 106  * provided arguments, or of the elements in the provided array.
 107  * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
 108  * Callers should make no assumptions about the identity of the returned instances.
 109  * Factories are free to create new instances or reuse existing ones. Therefore,
 110  * identity-sensitive operations on these instances (reference equality ({@code ==}),
 111  * identity hash code, and synchronization) are unreliable and should be avoided.
 112  * <li>They are serialized as specified on the
 113  * <a href="{@docRoot}/serialized-form.html#java.util.CollSer">Serialized Form</a>
 114  * page.
 115  * </ul>
 116  *
 117  * <p>This interface is a member of the
 118  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
 119  * Java Collections Framework</a>.
 120  *
 121  * @param <E> the type of elements in this list
 122  *
 123  * @author  Josh Bloch
 124  * @author  Neal Gafter
 125  * @see Collection
 126  * @see Set
 127  * @see ArrayList
 128  * @see LinkedList
 129  * @see Vector
 130  * @see Arrays#asList(Object[])
 131  * @see Collections#nCopies(int, Object)
 132  * @see Collections#EMPTY_LIST
 133  * @see AbstractList
 134  * @see AbstractSequentialList
 135  * @since 1.2
 136  */
 137 
 138 public interface List<E> extends Collection<E> {
 139     // Query Operations
 140 
 141     /**
 142      * Returns the number of elements in this list.  If this list contains
 143      * more than {@code Integer.MAX_VALUE} elements, returns
 144      * {@code Integer.MAX_VALUE}.
 145      *
 146      * @return the number of elements in this list
 147      */
 148     int size();
 149 
 150     /**
 151      * Returns {@code true} if this list contains no elements.
 152      *
 153      * @return {@code true} if this list contains no elements
 154      */
 155     boolean isEmpty();
 156 
 157     /**
 158      * Returns {@code true} if this list contains the specified element.
 159      * More formally, returns {@code true} if and only if this list contains
 160      * at least one element {@code e} such that
 161      * {@code Objects.equals(o, e)}.
 162      *
 163      * @param o element whose presence in this list is to be tested
 164      * @return {@code true} if this list contains the specified element
 165      * @throws ClassCastException if the type of the specified element
 166      *         is incompatible with this list
 167      * (<a href="Collection.html#optional-restrictions">optional</a>)
 168      * @throws NullPointerException if the specified element is null and this
 169      *         list does not permit null elements
 170      * (<a href="Collection.html#optional-restrictions">optional</a>)
 171      */
 172     boolean contains(Object o);
 173 
 174     /**
 175      * Returns an iterator over the elements in this list in proper sequence.
 176      *
 177      * @return an iterator over the elements in this list in proper sequence
 178      */
 179     Iterator<E> iterator();
 180 
 181     /**
 182      * Returns an array containing all of the elements in this list in proper
 183      * sequence (from first to last element).
 184      *
 185      * <p>The returned array will be "safe" in that no references to it are
 186      * maintained by this list.  (In other words, this method must
 187      * allocate a new array even if this list is backed by an array).
 188      * The caller is thus free to modify the returned array.
 189      *
 190      * <p>This method acts as bridge between array-based and collection-based
 191      * APIs.
 192      *
 193      * @return an array containing all of the elements in this list in proper
 194      *         sequence
 195      * @see Arrays#asList(Object[])
 196      */
 197     Object[] toArray();
 198 
 199     /**
 200      * Returns an array containing all of the elements in this list in
 201      * proper sequence (from first to last element); the runtime type of
 202      * the returned array is that of the specified array.  If the list fits
 203      * in the specified array, it is returned therein.  Otherwise, a new
 204      * array is allocated with the runtime type of the specified array and
 205      * the size of this list.
 206      *
 207      * <p>If the list fits in the specified array with room to spare (i.e.,
 208      * the array has more elements than the list), the element in the array
 209      * immediately following the end of the list is set to {@code null}.
 210      * (This is useful in determining the length of the list <i>only</i> if
 211      * the caller knows that the list does not contain any null elements.)
 212      *
 213      * <p>Like the {@link #toArray()} method, this method acts as bridge between
 214      * array-based and collection-based APIs.  Further, this method allows
 215      * precise control over the runtime type of the output array, and may,
 216      * under certain circumstances, be used to save allocation costs.
 217      *
 218      * <p>Suppose {@code x} is a list known to contain only strings.
 219      * The following code can be used to dump the list into a newly
 220      * allocated array of {@code String}:
 221      *
 222      * <pre>{@code
 223      *     String[] y = x.toArray(new String[0]);
 224      * }</pre>
 225      *
 226      * Note that {@code toArray(new Object[0])} is identical in function to
 227      * {@code toArray()}.
 228      *
 229      * @param a the array into which the elements of this list are to
 230      *          be stored, if it is big enough; otherwise, a new array of the
 231      *          same runtime type is allocated for this purpose.
 232      * @return an array containing the elements of this list
 233      * @throws ArrayStoreException if the runtime type of the specified array
 234      *         is not a supertype of the runtime type of every element in
 235      *         this list
 236      * @throws NullPointerException if the specified array is null
 237      */
 238     <T> T[] toArray(T[] a);
 239 
 240 
 241     // Modification Operations
 242 
 243     /**
 244      * Appends the specified element to the end of this list (optional
 245      * operation).
 246      *
 247      * <p>Lists that support this operation may place limitations on what
 248      * elements may be added to this list.  In particular, some
 249      * lists will refuse to add null elements, and others will impose
 250      * restrictions on the type of elements that may be added.  List
 251      * classes should clearly specify in their documentation any restrictions
 252      * on what elements may be added.
 253      *
 254      * @param e element to be appended to this list
 255      * @return {@code true} (as specified by {@link Collection#add})
 256      * @throws UnsupportedOperationException if the {@code add} operation
 257      *         is not supported by this list
 258      * @throws ClassCastException if the class of the specified element
 259      *         prevents it from being added to this list
 260      * @throws NullPointerException if the specified element is null and this
 261      *         list does not permit null elements
 262      * @throws IllegalArgumentException if some property of this element
 263      *         prevents it from being added to this list
 264      */
 265     boolean add(E e);
 266 
 267     /**
 268      * Removes the first occurrence of the specified element from this list,
 269      * if it is present (optional operation).  If this list does not contain
 270      * the element, it is unchanged.  More formally, removes the element with
 271      * the lowest index {@code i} such that
 272      * {@code Objects.equals(o, get(i))}
 273      * (if such an element exists).  Returns {@code true} if this list
 274      * contained the specified element (or equivalently, if this list changed
 275      * as a result of the call).
 276      *
 277      * @param o element to be removed from this list, if present
 278      * @return {@code true} if this list contained the specified element
 279      * @throws ClassCastException if the type of the specified element
 280      *         is incompatible with this list
 281      * (<a href="Collection.html#optional-restrictions">optional</a>)
 282      * @throws NullPointerException if the specified element is null and this
 283      *         list does not permit null elements
 284      * (<a href="Collection.html#optional-restrictions">optional</a>)
 285      * @throws UnsupportedOperationException if the {@code remove} operation
 286      *         is not supported by this list
 287      */
 288     boolean remove(Object o);
 289 
 290 
 291     // Bulk Modification Operations
 292 
 293     /**
 294      * Returns {@code true} if this list contains all of the elements of the
 295      * specified collection. This operation uses the membership semantics
 296      * of this list.
 297      *
 298      * @param  c collection to be checked for containment in this list
 299      * @return {@code true} if this list contains all of the elements of the
 300      *         specified collection
 301      * @throws ClassCastException if the types of one or more elements
 302      *         in the specified collection are incompatible with this
 303      *         list
 304      * (<a href="Collection.html#optional-restrictions">optional</a>)
 305      * @throws NullPointerException if the specified collection contains one
 306      *         or more null elements and this list does not permit null
 307      *         elements
 308      *         (<a href="Collection.html#optional-restrictions">optional</a>),
 309      *         or if the specified collection is null
 310      * @see #contains(Object)
 311      */
 312     boolean containsAll(Collection<?> c);
 313 
 314     /**
 315      * Appends all of the elements in the specified collection to the end of
 316      * this list, in the order that they are returned by the specified
 317      * collection's iterator (optional operation).  The behavior of this
 318      * operation is undefined if the specified collection is modified while
 319      * the operation is in progress.  (Note that this will occur if the
 320      * specified collection is this list, and it's nonempty.)
 321      *
 322      * @param c collection containing elements to be added to this list
 323      * @return {@code true} if this list changed as a result of the call
 324      * @throws UnsupportedOperationException if the {@code addAll} operation
 325      *         is not supported by this list
 326      * @throws ClassCastException if the class of an element of the specified
 327      *         collection prevents it from being added to this list
 328      * @throws NullPointerException if the specified collection contains one
 329      *         or more null elements and this list does not permit null
 330      *         elements, or if the specified collection is null
 331      * @throws IllegalArgumentException if some property of an element of the
 332      *         specified collection prevents it from being added to this list
 333      * @see #add(Object)
 334      */
 335     boolean addAll(Collection<? extends E> c);
 336 
 337     /**
 338      * Inserts all of the elements in the specified collection into this
 339      * list at the specified position (optional operation).  Shifts the
 340      * element currently at that position (if any) and any subsequent
 341      * elements to the right (increases their indices).  The new elements
 342      * will appear in this list in the order that they are returned by the
 343      * specified collection's iterator.  The behavior of this operation is
 344      * undefined if the specified collection is modified while the
 345      * operation is in progress.  (Note that this will occur if the specified
 346      * collection is this list, and it's nonempty.)
 347      *
 348      * @param index index at which to insert the first element from the
 349      *              specified collection
 350      * @param c collection containing elements to be added to this list
 351      * @return {@code true} if this list changed as a result of the call
 352      * @throws UnsupportedOperationException if the {@code addAll} operation
 353      *         is not supported by this list
 354      * @throws ClassCastException if the class of an element of the specified
 355      *         collection prevents it from being added to this list
 356      * @throws NullPointerException if the specified collection contains one
 357      *         or more null elements and this list does not permit null
 358      *         elements, or if the specified collection is null
 359      * @throws IllegalArgumentException if some property of an element of the
 360      *         specified collection prevents it from being added to this list
 361      * @throws IndexOutOfBoundsException if the index is out of range
 362      *         ({@code index < 0 || index > size()})
 363      */
 364     boolean addAll(int index, Collection<? extends E> c);
 365 
 366     /**
 367      * Removes from this list all of its elements that are contained in the
 368      * specified collection (optional operation). This operation uses the
 369      * membership semantics of the specified collection.
 370      *
 371      * @implNote
 372      * {@inheritDoc}
 373      *
 374      * @param c collection containing elements to be removed from this list
 375      * @return {@code true} if this list changed as a result of the call
 376      * @throws UnsupportedOperationException if the {@code removeAll} operation
 377      *         is not supported by this list
 378      * @throws ClassCastException if the class of an element of this list
 379      *         is incompatible with the specified collection
 380      * (<a href="Collection.html#optional-restrictions">optional</a>)
 381      * @throws NullPointerException if this list contains a null element and the
 382      *         specified collection does not permit null elements
 383      *         (<a href="Collection.html#optional-restrictions">optional</a>),
 384      *         or if the specified collection is null
 385      * @see #remove(Object)
 386      * @see #contains(Object)
 387      */
 388     boolean removeAll(Collection<?> c);
 389 
 390     /**
 391      * Retains only the elements in this list that are contained in the
 392      * specified collection (optional operation).  In other words, removes
 393      * from this list all of its elements that are not contained in the
 394      * specified collection. This operation uses the membership semantics of
 395      * the specified collection.
 396      *
 397      * @implNote
 398      * {@inheritDoc}
 399      *
 400      * @param c collection containing elements to be retained in this list
 401      * @return {@code true} if this list changed as a result of the call
 402      * @throws UnsupportedOperationException if the {@code retainAll} operation
 403      *         is not supported by this list
 404      * @throws ClassCastException if the class of an element of this list
 405      *         is incompatible with the specified collection
 406      * (<a href="Collection.html#optional-restrictions">optional</a>)
 407      * @throws NullPointerException if this list contains a null element and the
 408      *         specified collection does not permit null elements
 409      *         (<a href="Collection.html#optional-restrictions">optional</a>),
 410      *         or if the specified collection is null
 411      * @see #remove(Object)
 412      * @see #contains(Object)
 413      */
 414     boolean retainAll(Collection<?> c);
 415 
 416     /**
 417      * Replaces each element of this list with the result of applying the
 418      * operator to that element.  Errors or runtime exceptions thrown by
 419      * the operator are relayed to the caller.
 420      *
 421      * @implSpec
 422      * The default implementation is equivalent to, for this {@code list}:
 423      * <pre>{@code
 424      *     final ListIterator<E> li = list.listIterator();
 425      *     while (li.hasNext()) {
 426      *         li.set(operator.apply(li.next()));
 427      *     }
 428      * }</pre>
 429      *
 430      * If the list's list-iterator does not support the {@code set} operation
 431      * then an {@code UnsupportedOperationException} will be thrown when
 432      * replacing the first element.
 433      *
 434      * @param operator the operator to apply to each element
 435      * @throws UnsupportedOperationException if this list is unmodifiable.
 436      *         Implementations may throw this exception if an element
 437      *         cannot be replaced or if, in general, modification is not
 438      *         supported
 439      * @throws NullPointerException if the specified operator is null or
 440      *         if the operator result is a null value and this list does
 441      *         not permit null elements
 442      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 443      * @since 1.8
 444      */
 445     default void replaceAll(UnaryOperator<E> operator) {
 446         Objects.requireNonNull(operator);
 447         final ListIterator<E> li = this.listIterator();
 448         while (li.hasNext()) {
 449             li.set(operator.apply(li.next()));
 450         }
 451     }
 452 
 453     /**
 454      * Sorts this list according to the order induced by the specified
 455      * {@link Comparator}.  The sort is <i>stable</i>: this method must not
 456      * reorder equal elements.
 457      *
 458      * <p>All elements in this list must be <i>mutually comparable</i> using the
 459      * specified comparator (that is, {@code c.compare(e1, e2)} must not throw
 460      * a {@code ClassCastException} for any elements {@code e1} and {@code e2}
 461      * in the list).
 462      *
 463      * <p>If the specified comparator is {@code null} then all elements in this
 464      * list must implement the {@link Comparable} interface and the elements'
 465      * {@linkplain Comparable natural ordering} should be used.
 466      *
 467      * <p>This list must be modifiable, but need not be resizable.
 468      *
 469      * @implSpec
 470      * The default implementation obtains an array containing all elements in
 471      * this list, sorts the array, and iterates over this list resetting each
 472      * element from the corresponding position in the array. (This avoids the
 473      * n<sup>2</sup> log(n) performance that would result from attempting
 474      * to sort a linked list in place.)
 475      *
 476      * @implNote
 477      * This implementation is a stable, adaptive, iterative mergesort that
 478      * requires far fewer than n lg(n) comparisons when the input array is
 479      * partially sorted, while offering the performance of a traditional
 480      * mergesort when the input array is randomly ordered.  If the input array
 481      * is nearly sorted, the implementation requires approximately n
 482      * comparisons.  Temporary storage requirements vary from a small constant
 483      * for nearly sorted input arrays to n/2 object references for randomly
 484      * ordered input arrays.
 485      *
 486      * <p>The implementation takes equal advantage of ascending and
 487      * descending order in its input array, and can take advantage of
 488      * ascending and descending order in different parts of the same
 489      * input array.  It is well-suited to merging two or more sorted arrays:
 490      * simply concatenate the arrays and sort the resulting array.
 491      *
 492      * <p>The implementation was adapted from Tim Peters's list sort for Python
 493      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
 494      * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
 495      * Sorting and Information Theoretic Complexity", in Proceedings of the
 496      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
 497      * January 1993.
 498      *
 499      * @param c the {@code Comparator} used to compare list elements.
 500      *          A {@code null} value indicates that the elements'
 501      *          {@linkplain Comparable natural ordering} should be used
 502      * @throws ClassCastException if the list contains elements that are not
 503      *         <i>mutually comparable</i> using the specified comparator
 504      * @throws UnsupportedOperationException if the list's list-iterator does
 505      *         not support the {@code set} operation
 506      * @throws IllegalArgumentException
 507      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 508      *         if the comparator is found to violate the {@link Comparator}
 509      *         contract
 510      * @since 1.8
 511      */
 512     @SuppressWarnings({"unchecked", "rawtypes"})
 513     default void sort(Comparator<? super E> c) {
 514         Object[] a = this.toArray();
 515         Arrays.sort(a, (Comparator) c);
 516         ListIterator<E> i = this.listIterator();
 517         for (Object e : a) {
 518             i.next();
 519             i.set((E) e);
 520         }
 521     }
 522 
 523     /**
 524      * Removes all of the elements from this list (optional operation).
 525      * The list will be empty after this call returns.
 526      *
 527      * @throws UnsupportedOperationException if the {@code clear} operation
 528      *         is not supported by this list
 529      */
 530     void clear();
 531 
 532 
 533     // Comparison and hashing
 534 
 535     /**
 536      * Compares the specified object with this list for equality.  Returns
 537      * {@code true} if and only if the specified object is also a list, both
 538      * lists have the same size, and all corresponding pairs of elements in
 539      * the two lists are <i>equal</i>.  (Two elements {@code e1} and
 540      * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
 541      * e1.equals(e2))}.)  In other words, two lists are defined to be
 542      * equal if they contain the same elements in the same order.  This
 543      * definition ensures that the {@code equals} method works properly across
 544      * different implementations of the {@code List} interface.
 545      *
 546      * @param o the object to be compared for equality with this list
 547      * @return {@code true} if the specified object is equal to this list
 548      */
 549     boolean equals(Object o);
 550 
 551     /**
 552      * Returns the hash code value for this list.  The hash code of a list
 553      * is defined to be the result of the following calculation:
 554      * <pre>{@code
 555      *     int hashCode = 1;
 556      *     for (E e : list)
 557      *         hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
 558      * }</pre>
 559      * This ensures that {@code list1.equals(list2)} implies that
 560      * {@code list1.hashCode()==list2.hashCode()} for any two lists,
 561      * {@code list1} and {@code list2}, as required by the general
 562      * contract of {@link Object#hashCode}.
 563      *
 564      * @return the hash code value for this list
 565      * @see Object#equals(Object)
 566      * @see #equals(Object)
 567      */
 568     int hashCode();
 569 
 570 
 571     // Positional Access Operations
 572 
 573     /**
 574      * Returns the element at the specified position in this list.
 575      *
 576      * @param index index of the element to return
 577      * @return the element at the specified position in this list
 578      * @throws IndexOutOfBoundsException if the index is out of range
 579      *         ({@code index < 0 || index >= size()})
 580      */
 581     E get(int index);
 582 
 583     /**
 584      * Replaces the element at the specified position in this list with the
 585      * specified element (optional operation).
 586      *
 587      * @param index index of the element to replace
 588      * @param element element to be stored at the specified position
 589      * @return the element previously at the specified position
 590      * @throws UnsupportedOperationException if the {@code set} operation
 591      *         is not supported by this list
 592      * @throws ClassCastException if the class of the specified element
 593      *         prevents it from being added to this list
 594      * @throws NullPointerException if the specified element is null and
 595      *         this list does not permit null elements
 596      * @throws IllegalArgumentException if some property of the specified
 597      *         element prevents it from being added to this list
 598      * @throws IndexOutOfBoundsException if the index is out of range
 599      *         ({@code index < 0 || index >= size()})
 600      */
 601     E set(int index, E element);
 602 
 603     /**
 604      * Inserts the specified element at the specified position in this list
 605      * (optional operation).  Shifts the element currently at that position
 606      * (if any) and any subsequent elements to the right (adds one to their
 607      * indices).
 608      *
 609      * @param index index at which the specified element is to be inserted
 610      * @param element element to be inserted
 611      * @throws UnsupportedOperationException if the {@code add} operation
 612      *         is not supported by this list
 613      * @throws ClassCastException if the class of the specified element
 614      *         prevents it from being added to this list
 615      * @throws NullPointerException if the specified element is null and
 616      *         this list does not permit null elements
 617      * @throws IllegalArgumentException if some property of the specified
 618      *         element prevents it from being added to this list
 619      * @throws IndexOutOfBoundsException if the index is out of range
 620      *         ({@code index < 0 || index > size()})
 621      */
 622     void add(int index, E element);
 623 
 624     /**
 625      * Removes the element at the specified position in this list (optional
 626      * operation).  Shifts any subsequent elements to the left (subtracts one
 627      * from their indices).  Returns the element that was removed from the
 628      * list.
 629      *
 630      * @param index the index of the element to be removed
 631      * @return the element previously at the specified position
 632      * @throws UnsupportedOperationException if the {@code remove} operation
 633      *         is not supported by this list
 634      * @throws IndexOutOfBoundsException if the index is out of range
 635      *         ({@code index < 0 || index >= size()})
 636      */
 637     E remove(int index);
 638 
 639 
 640     // Search Operations
 641 
 642     /**
 643      * Returns the index of the first occurrence of the specified element
 644      * in this list, or -1 if this list does not contain the element.
 645      * More formally, returns the lowest index {@code i} such that
 646      * {@code Objects.equals(o, get(i))},
 647      * or -1 if there is no such index.
 648      *
 649      * @param o element to search for
 650      * @return the index of the first occurrence of the specified element in
 651      *         this list, or -1 if this list does not contain the element
 652      * @throws ClassCastException if the type of the specified element
 653      *         is incompatible with this list
 654      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 655      * @throws NullPointerException if the specified element is null and this
 656      *         list does not permit null elements
 657      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 658      */
 659     int indexOf(Object o);
 660 
 661     /**
 662      * Returns the index of the last occurrence of the specified element
 663      * in this list, or -1 if this list does not contain the element.
 664      * More formally, returns the highest index {@code i} such that
 665      * {@code Objects.equals(o, get(i))},
 666      * or -1 if there is no such index.
 667      *
 668      * @param o element to search for
 669      * @return the index of the last occurrence of the specified element in
 670      *         this list, or -1 if this list does not contain the element
 671      * @throws ClassCastException if the type of the specified element
 672      *         is incompatible with this list
 673      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 674      * @throws NullPointerException if the specified element is null and this
 675      *         list does not permit null elements
 676      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 677      */
 678     int lastIndexOf(Object o);
 679 
 680 
 681     // List Iterators
 682 
 683     /**
 684      * Returns a list iterator over the elements in this list (in proper
 685      * sequence).
 686      *
 687      * @return a list iterator over the elements in this list (in proper
 688      *         sequence)
 689      */
 690     ListIterator<E> listIterator();
 691 
 692     /**
 693      * Returns a list iterator over the elements in this list (in proper
 694      * sequence), starting at the specified position in the list.
 695      * The specified index indicates the first element that would be
 696      * returned by an initial call to {@link ListIterator#next next}.
 697      * An initial call to {@link ListIterator#previous previous} would
 698      * return the element with the specified index minus one.
 699      *
 700      * @param index index of the first element to be returned from the
 701      *        list iterator (by a call to {@link ListIterator#next next})
 702      * @return a list iterator over the elements in this list (in proper
 703      *         sequence), starting at the specified position in the list
 704      * @throws IndexOutOfBoundsException if the index is out of range
 705      *         ({@code index < 0 || index > size()})
 706      */
 707     ListIterator<E> listIterator(int index);
 708 
 709     // View
 710 
 711     /**
 712      * Returns a view of the portion of this list between the specified
 713      * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.  (If
 714      * {@code fromIndex} and {@code toIndex} are equal, the returned list is
 715      * empty.)  The returned list is backed by this list, so non-structural
 716      * changes in the returned list are reflected in this list, and vice-versa.
 717      * The returned list supports all of the optional list operations supported
 718      * by this list.<p>
 719      *
 720      * This method eliminates the need for explicit range operations (of
 721      * the sort that commonly exist for arrays).  Any operation that expects
 722      * a list can be used as a range operation by passing a subList view
 723      * instead of a whole list.  For example, the following idiom
 724      * removes a range of elements from a list:
 725      * <pre>{@code
 726      *      list.subList(from, to).clear();
 727      * }</pre>
 728      * Similar idioms may be constructed for {@code indexOf} and
 729      * {@code lastIndexOf}, and all of the algorithms in the
 730      * {@code Collections} class can be applied to a subList.<p>
 731      *
 732      * The semantics of the list returned by this method become undefined if
 733      * the backing list (i.e., this list) is <i>structurally modified</i> in
 734      * any way other than via the returned list.  (Structural modifications are
 735      * those that change the size of this list, or otherwise perturb it in such
 736      * a fashion that iterations in progress may yield incorrect results.)
 737      *
 738      * @param fromIndex low endpoint (inclusive) of the subList
 739      * @param toIndex high endpoint (exclusive) of the subList
 740      * @return a view of the specified range within this list
 741      * @throws IndexOutOfBoundsException for an illegal endpoint index value
 742      *         ({@code fromIndex < 0 || toIndex > size ||
 743      *         fromIndex > toIndex})
 744      */
 745     List<E> subList(int fromIndex, int toIndex);
 746 
 747     /**
 748      * Creates a {@link Spliterator} over the elements in this list.
 749      *
 750      * <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
 751      * {@link Spliterator#ORDERED}.  Implementations should document the
 752      * reporting of additional characteristic values.
 753      *
 754      * @implSpec
 755      * The default implementation creates a
 756      * <em><a href="Spliterator.html#binding">late-binding</a></em>
 757      * spliterator as follows:
 758      * <ul>
 759      * <li>If the list is an instance of {@link RandomAccess} then the default
 760      *     implementation creates a spliterator that traverses elements by
 761      *     invoking the method {@link List#get}.  If such invocation results or
 762      *     would result in an {@code IndexOutOfBoundsException} then the
 763      *     spliterator will <em>fail-fast</em> and throw a
 764      *     {@code ConcurrentModificationException}.
 765      *     If the list is also an instance of {@link AbstractList} then the
 766      *     spliterator will use the list's {@link AbstractList#modCount modCount}
 767      *     field to provide additional <em>fail-fast</em> behavior.
 768      * <li>Otherwise, the default implementation creates a spliterator from the
 769      *     list's {@code Iterator}.  The spliterator inherits the
 770      *     <em>fail-fast</em> of the list's iterator.
 771      * </ul>
 772      *
 773      * @implNote
 774      * The created {@code Spliterator} additionally reports
 775      * {@link Spliterator#SUBSIZED}.
 776      *
 777      * @return a {@code Spliterator} over the elements in this list
 778      * @since 1.8
 779      */
 780     @Override
 781     default Spliterator<E> spliterator() {
 782         if (this instanceof RandomAccess) {
 783             return new AbstractList.RandomAccessSpliterator<>(this);
 784         } else {
 785             return Spliterators.spliterator(this, Spliterator.ORDERED);
 786         }
 787     }
 788 
 789     /**
 790      * Returns an unmodifiable list containing zero elements.
 791      *
 792      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 793      *
 794      * @param <E> the {@code List}'s element type
 795      * @return an empty {@code List}
 796      *
 797      * @since 9
 798      */
 799     @SuppressWarnings("unchecked")
 800     static <E> List<E> of() {
 801         return (List<E>) ImmutableCollections.ListN.EMPTY_LIST;
 802     }
 803 
 804     /**
 805      * Returns an unmodifiable list containing one element.
 806      *
 807      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 808      *
 809      * @param <E> the {@code List}'s element type
 810      * @param e1 the single element
 811      * @return a {@code List} containing the specified element
 812      * @throws NullPointerException if the element is {@code null}
 813      *
 814      * @since 9
 815      */
 816     static <E> List<E> of(E e1) {
 817         return new ImmutableCollections.List12<>(e1);
 818     }
 819 
 820     /**
 821      * Returns an unmodifiable list containing two elements.
 822      *
 823      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 824      *
 825      * @param <E> the {@code List}'s element type
 826      * @param e1 the first element
 827      * @param e2 the second element
 828      * @return a {@code List} containing the specified elements
 829      * @throws NullPointerException if an element is {@code null}
 830      *
 831      * @since 9
 832      */
 833     static <E> List<E> of(E e1, E e2) {
 834         return new ImmutableCollections.List12<>(e1, e2);
 835     }
 836 
 837     /**
 838      * Returns an unmodifiable list containing three elements.
 839      *
 840      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 841      *
 842      * @param <E> the {@code List}'s element type
 843      * @param e1 the first element
 844      * @param e2 the second element
 845      * @param e3 the third element
 846      * @return a {@code List} containing the specified elements
 847      * @throws NullPointerException if an element is {@code null}
 848      *
 849      * @since 9
 850      */
 851     static <E> List<E> of(E e1, E e2, E e3) {
 852         return new ImmutableCollections.ListN<>(e1, e2, e3);
 853     }
 854 
 855     /**
 856      * Returns an unmodifiable list containing four elements.
 857      *
 858      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 859      *
 860      * @param <E> the {@code List}'s element type
 861      * @param e1 the first element
 862      * @param e2 the second element
 863      * @param e3 the third element
 864      * @param e4 the fourth element
 865      * @return a {@code List} containing the specified elements
 866      * @throws NullPointerException if an element is {@code null}
 867      *
 868      * @since 9
 869      */
 870     static <E> List<E> of(E e1, E e2, E e3, E e4) {
 871         return new ImmutableCollections.ListN<>(e1, e2, e3, e4);
 872     }
 873 
 874     /**
 875      * Returns an unmodifiable list containing five elements.
 876      *
 877      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 878      *
 879      * @param <E> the {@code List}'s element type
 880      * @param e1 the first element
 881      * @param e2 the second element
 882      * @param e3 the third element
 883      * @param e4 the fourth element
 884      * @param e5 the fifth element
 885      * @return a {@code List} containing the specified elements
 886      * @throws NullPointerException if an element is {@code null}
 887      *
 888      * @since 9
 889      */
 890     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
 891         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5);
 892     }
 893 
 894     /**
 895      * Returns an unmodifiable list containing six elements.
 896      *
 897      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 898      *
 899      * @param <E> the {@code List}'s element type
 900      * @param e1 the first element
 901      * @param e2 the second element
 902      * @param e3 the third element
 903      * @param e4 the fourth element
 904      * @param e5 the fifth element
 905      * @param e6 the sixth element
 906      * @return a {@code List} containing the specified elements
 907      * @throws NullPointerException if an element is {@code null}
 908      *
 909      * @since 9
 910      */
 911     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
 912         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 913                                                 e6);
 914     }
 915 
 916     /**
 917      * Returns an unmodifiable list containing seven elements.
 918      *
 919      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 920      *
 921      * @param <E> the {@code List}'s element type
 922      * @param e1 the first element
 923      * @param e2 the second element
 924      * @param e3 the third element
 925      * @param e4 the fourth element
 926      * @param e5 the fifth element
 927      * @param e6 the sixth element
 928      * @param e7 the seventh element
 929      * @return a {@code List} containing the specified elements
 930      * @throws NullPointerException if an element is {@code null}
 931      *
 932      * @since 9
 933      */
 934     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
 935         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 936                                                 e6, e7);
 937     }
 938 
 939     /**
 940      * Returns an unmodifiable list containing eight elements.
 941      *
 942      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 943      *
 944      * @param <E> the {@code List}'s element type
 945      * @param e1 the first element
 946      * @param e2 the second element
 947      * @param e3 the third element
 948      * @param e4 the fourth element
 949      * @param e5 the fifth element
 950      * @param e6 the sixth element
 951      * @param e7 the seventh element
 952      * @param e8 the eighth element
 953      * @return a {@code List} containing the specified elements
 954      * @throws NullPointerException if an element is {@code null}
 955      *
 956      * @since 9
 957      */
 958     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
 959         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 960                                                 e6, e7, e8);
 961     }
 962 
 963     /**
 964      * Returns an unmodifiable list containing nine elements.
 965      *
 966      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 967      *
 968      * @param <E> the {@code List}'s element type
 969      * @param e1 the first element
 970      * @param e2 the second element
 971      * @param e3 the third element
 972      * @param e4 the fourth element
 973      * @param e5 the fifth element
 974      * @param e6 the sixth element
 975      * @param e7 the seventh element
 976      * @param e8 the eighth element
 977      * @param e9 the ninth element
 978      * @return a {@code List} containing the specified elements
 979      * @throws NullPointerException if an element is {@code null}
 980      *
 981      * @since 9
 982      */
 983     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
 984         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 985                                                 e6, e7, e8, e9);
 986     }
 987 
 988     /**
 989      * Returns an unmodifiable list containing ten elements.
 990      *
 991      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 992      *
 993      * @param <E> the {@code List}'s element type
 994      * @param e1 the first element
 995      * @param e2 the second element
 996      * @param e3 the third element
 997      * @param e4 the fourth element
 998      * @param e5 the fifth element
 999      * @param e6 the sixth element
1000      * @param e7 the seventh element
1001      * @param e8 the eighth element
1002      * @param e9 the ninth element
1003      * @param e10 the tenth element
1004      * @return a {@code List} containing the specified elements
1005      * @throws NullPointerException if an element is {@code null}
1006      *
1007      * @since 9
1008      */
1009     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
1010         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
1011                                                 e6, e7, e8, e9, e10);
1012     }
1013 
1014     /**
1015      * Returns an unmodifiable list containing an arbitrary number of elements.
1016      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
1017      *
1018      * @apiNote
1019      * This method also accepts a single array as an argument. The element type of
1020      * the resulting list will be the component type of the array, and the size of
1021      * the list will be equal to the length of the array. To create a list with
1022      * a single element that is an array, do the following:
1023      *
1024      * <pre>{@code
1025      *     String[] array = ... ;
1026      *     List<String[]> list = List.<String[]>of(array);
1027      * }</pre>
1028      *
1029      * This will cause the {@link List#of(Object) List.of(E)} method
1030      * to be invoked instead.
1031      *
1032      * @param <E> the {@code List}'s element type
1033      * @param elements the elements to be contained in the list
1034      * @return a {@code List} containing the specified elements
1035      * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
1036      *
1037      * @since 9
1038      */
1039     @SafeVarargs
1040     @SuppressWarnings("varargs")
1041     static <E> List<E> of(E... elements) {
1042         switch (elements.length) { // implicit null check of elements
1043             case 0:
1044                 @SuppressWarnings("unchecked")
1045                 var list = (List<E>) ImmutableCollections.ListN.EMPTY_LIST;
1046                 return list;
1047             case 1:
1048                 return new ImmutableCollections.List12<>(elements[0]);
1049             case 2:
1050                 return new ImmutableCollections.List12<>(elements[0], elements[1]);
1051             default:
1052                 return new ImmutableCollections.ListN<>(elements);
1053         }
1054     }
1055 
1056     /**
1057      * Returns an <a href="#unmodifiable">unmodifiable List</a> containing the elements of
1058      * the given Collection, in its iteration order. The given Collection must not be null,
1059      * and it must not contain any null elements. If the given Collection is subsequently
1060      * modified, the returned List will not reflect such modifications.
1061      *
1062      * @implNote
1063      * If the given Collection is an <a href="#unmodifiable">unmodifiable List</a>,
1064      * calling copyOf will generally not create a copy.
1065      *
1066      * @param <E> the {@code List}'s element type
1067      * @param coll a {@code Collection} from which elements are drawn, must be non-null
1068      * @return a {@code List} containing the elements of the given {@code Collection}
1069      * @throws NullPointerException if coll is null, or if it contains any nulls
1070      * @since 10
1071      */
1072     static <E> List<E> copyOf(Collection<? extends E> coll) {
1073         return ImmutableCollections.listCopy(coll);
1074     }
1075 }