< prev index next >

src/java.base/share/classes/java/util/Collection.java

Print this page

        

*** 21,35 **** * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ ! package java.util; ! import java.util.function.Predicate; ! import java.util.stream.Stream; ! import java.util.stream.StreamSupport; /** * The root interface in the <i>collection hierarchy</i>. A collection * represents a group of objects, known as its <i>elements</i>. Some * collections allow duplicate elements and others do not. Some are ordered --- 21,35 ---- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ ! package javany.util; ! import javany.util.function.Predicate; ! ! import java.util.Objects; /** * The root interface in the <i>collection hierarchy</i>. A collection * represents a group of objects, known as its <i>elements</i>. Some * collections allow duplicate elements and others do not. Some are ordered
*** 139,149 **** * @see Arrays * @see AbstractCollection * @since 1.2 */ ! public interface Collection<E> extends Iterable<E> { // Query Operations /** * Returns the number of elements in this collection. If this collection * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns --- 139,149 ---- * @see Arrays * @see AbstractCollection * @since 1.2 */ ! public interface Collection<any E> extends javany.lang.Iterable<E> { // Query Operations /** * Returns the number of elements in this collection. If this collection * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
*** 176,185 **** --- 176,193 ---- * collection does not permit null elements * (<a href="#optional-restrictions">optional</a>) */ boolean contains(Object o); + default boolean containsElement(E e) { + Iterator<E> it = iterator(); + while (it.hasNext()) + if (Any.equals(e, it.next())) + return true; + return false; + } + /** * Returns an iterator over the elements in this collection. There are no * guarantees concerning the order in which the elements are returned * (unless this collection is an instance of some class that provides a * guarantee).
*** 247,257 **** * @throws ArrayStoreException if the runtime type of the specified array * is not a supertype of the runtime type of every element in * this collection * @throws NullPointerException if the specified array is null */ ! <T> T[] toArray(T[] a); // Modification Operations /** * Ensures that this collection contains the specified element (optional --- 255,265 ---- * @throws ArrayStoreException if the runtime type of the specified array * is not a supertype of the runtime type of every element in * this collection * @throws NullPointerException if the specified array is null */ ! <any T> T[] toArray(T[] a); // Modification Operations /** * Ensures that this collection contains the specified element (optional
*** 308,317 **** --- 316,335 ---- * @throws UnsupportedOperationException if the <tt>remove</tt> operation * is not supported by this collection */ boolean remove(Object o); + default boolean removeElement(E e) { + Iterator<E> it = iterator(); + while (it.hasNext()) { + if (Any.equals(e, it.next())) { + it.remove(); + return true; + } + } + return false; + } // Bulk Operations /** * Returns <tt>true</tt> if this collection contains all of the elements
*** 331,340 **** --- 349,368 ---- * or if the specified collection is null. * @see #contains(Object) */ boolean containsAll(Collection<?> c); + default boolean containsAllElements(Collection<? extends E> c) { + Iterator<? extends E> it = c.iterator(); + while (it.hasNext()) { + if (!containsElement(it.next())) { + return false; + } + } + return true; + } + /** * Adds all of the elements in the specified collection to this collection * (optional operation). The behavior of this operation is undefined if * the specified collection is modified while the operation is in progress. * (This implies that the behavior of this call is undefined if the
*** 382,391 **** --- 410,432 ---- * @see #remove(Object) * @see #contains(Object) */ boolean removeAll(Collection<?> c); + default boolean removeAllElements(Collection<E> c) { + Objects.requireNonNull(c); + boolean modified = false; + Iterator<E> it = iterator(); + while (it.hasNext()) { + if (c.containsElement(it.next())) { + it.remove(); + modified = true; + } + } + return modified; + } + /** * Removes all of the elements of this collection that satisfy the given * predicate. Errors or runtime exceptions thrown during iteration or by * the predicate are relayed to the caller. *
*** 441,450 **** --- 482,504 ---- * @see #remove(Object) * @see #contains(Object) */ boolean retainAll(Collection<?> c); + default boolean retainAllElements(Collection<E> c) { + Objects.requireNonNull(c); + boolean modified = false; + Iterator<E> it = iterator(); + while (it.hasNext()) { + if (!c.containsElement(it.next())) { + it.remove(); + modified = true; + } + } + return modified; + } + /** * Removes all of the elements from this collection (optional operation). * The collection will be empty after this method returns. * * @throws UnsupportedOperationException if the <tt>clear</tt> operation
*** 555,568 **** * covers no elements. * * @return a {@code Spliterator} over the elements in this collection * @since 1.8 */ ! @Override ! default Spliterator<E> spliterator() { ! return Spliterators.spliterator(this, 0); ! } /** * Returns a sequential {@code Stream} with this collection as its source. * * <p>This method should be overridden when the {@link #spliterator()} --- 609,622 ---- * covers no elements. * * @return a {@code Spliterator} over the elements in this collection * @since 1.8 */ ! // @Override ! // default Spliterator<E> spliterator() { ! // return Spliterators.spliterator(this, 0); ! // } /** * Returns a sequential {@code Stream} with this collection as its source. * * <p>This method should be overridden when the {@link #spliterator()}
*** 575,587 **** * collection's {@code Spliterator}. * * @return a sequential {@code Stream} over the elements in this collection * @since 1.8 */ ! default Stream<E> stream() { ! return StreamSupport.stream(spliterator(), false); ! } /** * Returns a possibly parallel {@code Stream} with this collection as its * source. It is allowable for this method to return a sequential stream. * --- 629,641 ---- * collection's {@code Spliterator}. * * @return a sequential {@code Stream} over the elements in this collection * @since 1.8 */ ! // default Stream<E> stream() { ! // return StreamSupport.stream(spliterator(), false); ! // } /** * Returns a possibly parallel {@code Stream} with this collection as its * source. It is allowable for this method to return a sequential stream. *
*** 596,604 **** * * @return a possibly parallel {@code Stream} over the elements in this * collection * @since 1.8 */ ! default Stream<E> parallelStream() { ! return StreamSupport.stream(spliterator(), true); ! } } --- 650,658 ---- * * @return a possibly parallel {@code Stream} over the elements in this * collection * @since 1.8 */ ! // default Stream<E> parallelStream() { ! // return StreamSupport.stream(spliterator(), true); ! // } }
< prev index next >