< prev index next >
src/java.base/share/classes/java/util/Collection.java
Print this page
@@ -21,15 +21,15 @@
* 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;
+package javany.util;
-import java.util.function.Predicate;
-import java.util.stream.Stream;
-import java.util.stream.StreamSupport;
+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,11 +139,11 @@
* @see Arrays
* @see AbstractCollection
* @since 1.2
*/
-public interface Collection<E> extends Iterable<E> {
+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,10 +176,18 @@
* 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,11 +255,11 @@
* @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);
+ <any T> T[] toArray(T[] a);
// Modification Operations
/**
* Ensures that this collection contains the specified element (optional
@@ -308,10 +316,20 @@
* @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,10 +349,20 @@
* 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,10 +410,23 @@
* @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,10 +482,23 @@
* @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,14 +609,14 @@
* 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);
- }
+// @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,13 +629,13 @@
* 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);
- }
+// 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,9 +650,9 @@
*
* @return a possibly parallel {@code Stream} over the elements in this
* collection
* @since 1.8
*/
- default Stream<E> parallelStream() {
- return StreamSupport.stream(spliterator(), true);
- }
+// default Stream<E> parallelStream() {
+// return StreamSupport.stream(spliterator(), true);
+// }
}
< prev index next >