src/share/classes/java/util/List.java

Print this page
rev 6962 : [mq]: collections

*** 23,32 **** --- 23,34 ---- * questions. */ package java.util; + import java.util.function.UnaryOperator; + /** * An ordered collection (also known as a <i>sequence</i>). The user of this * interface has precise control over where in the list each element is * inserted. The user can access elements by their integer index (position in * the list), and search for elements in the list.<p>
*** 373,382 **** --- 375,440 ---- * @see #contains(Object) */ boolean retainAll(Collection<?> c); /** + * Replaces each element of this list with the result of applying the + * operator to that element. Exceptions thrown by the operator are relayed + * to the caller. + * + * @implSpec + * The default implementation is equivalent to, for this {@code list}: + * <pre> + * final ListIterator<E> li = list.listIterator(); + * while (li.hasNext()) { + * li.set(operator.apply(li.next())); + * } + * </pre> + * If the list's list-iterator does not support the {@code set} operation + * then an {@code UnsupportedOperationException} will be thrown when + * replacing the first element. + * + * @param operator the operator to apply to each element + * @throws UnsupportedOperationException if the <code>replaceAll</code> + * operation is not supported by this list + * @throws NullPointerException if the specified operator is null + * @throws NullPointerException if the an element is replaced with a null + * value and this list does not permit null elements + * (<a href="Collection.html#optional-restrictions">optional</a>) + * @since 1.8 + */ + default void replaceAll(UnaryOperator<E> operator) { + Objects.requireNonNull(operator); + final ListIterator<E> li = this.listIterator(); + while (li.hasNext()) { + li.set(operator.apply(li.next())); + } + } + + /** + * Sorts this list using the supplied {@code Comparator} to compare elements. + * + * @implSpec + * The default implementation is equivalent to, for this {@code list}: + * <pre>Collections.sort(list, c)</pre> + * + * @param c the {@code Comparator} used to compare list elements. + * A {@code null} value indicates that the elements' + * {@linkplain Comparable natural ordering} should be used. + * @since 1.8 + * @throws ClassCastException if the list contains elements that are not + * <i>mutually comparable</i> using the specified comparator. + * @throws UnsupportedOperationException if the list's list-iterator does + * not support the {@code set} operation. + * @throws IllegalArgumentException (optional) if the comparator is + * found to violate the {@link Comparator} contract + */ + default void sort(Comparator<? super E> c) { + Collections.sort(this, c); + } + + /** * Removes all of the elements from this list (optional operation). * The list will be empty after this call returns. * * @throws UnsupportedOperationException if the <tt>clear</tt> operation * is not supported by this list