package java.util; import java.util.function.Predicate; import java.util.stream.Stream; import java.util.stream.StreamSupport; public interface Collection<E> extends Iterable<E> { // query operation /** * @return the number of elements in this collection */ int size(); /** * Check if the collection is empty */ boolean isEmpty(); /** * Determines that this collection contains the specified element. */ boolean contains(Object o); /** * */ Iterator<E> iterator(); /** * Returns an array containing all elements in this collection */ Object[] toArray(); /** * Returns an array containing all elements in this collection */ <T> T[] toArray(T[] a); // modify operation /** * add a single element */ boolean add(E e); /** *Single removal */ boolean remove(Object o); // batch operation /** * Determines that this collection contains the specified element. */ boolean containsAll(Collection<?> c); /** * add collection */ boolean addAll(Collection<? extends E> c); /** * remove collection */ boolean removeAll(Collection<?> c); /** * Safe to remove * @since 1.8 */ default boolean removeIf(Predicate<? super E> filter) { Objects.requireNonNull(filter); boolean removed = false; final Iterator<E> each = iterator(); while (each.hasNext()) { if (filter.test(each.next())) { each.remove(); removed = true; } } return removed; } /** * Take the intersection method retainAll * There are two sets newCoures and oldCourses, to determine whether the two sets contain the same object or element */ boolean retainAll(Collection<?> c); /** * remove all elements, making the collection empty */ void clear(); // Compare and hash /** * Compares the specified object with this collection for equality. */ boolean equals(Object o); /** * Find the hashCode of the collection */ int hashCode(); /** * Parallel traversal * * @return a {@code Spliterator} over the elements in this collection * @since 1.8 */ @Override default Spliterator<E> spliterator() { return Spliterators.spliterator(this, 0); } /** * https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/index.html * @since 1.8 */ default Stream<E> stream() { return StreamSupport.stream(spliterator(), false); } /** * https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/index.html * @since 1.8 */ default Stream<E> parallelStream() { return StreamSupport.stream(spliterator(), true); } }