Introduction to Collection interface

  1. A Collection represents a Set of objects and is the root interface in the Collection system. Some elements are allowed to have repetition, some are not allowed, some are ordered and some are not. JDK does not provide the direct implementation of concrete classes of this interface, but only the implementation of sub interfaces and abstract classes (such as List and Set). This interface is usually used to pass collections and when the maximum generality is needed (such as when the interface is used as a parameter);
  2. All implementation classes of this interface should provide a parameterless constructor and a parameterless constructor with Collection and parameters. This is not required by syntax but by java specification, because the interface cannot contain constructors;
  3. If this interface implementation class does not support a method, it can throw unsupported operation exception;
  4. Different implementation classes may have different restrictions on the elements they contain, such as some do not allow adding null, and some have restrictions on element types. Trying to add unqualified elements may throw NullPointerException or ClassCastException. Trying to query an unqualified element will throw an exception or return false, which depends on different implementations;
  5. The synchronization strategy is determined by each implementation class;

Query operation:

/**
     * Returns the number of elements in the collection. If the number of elements exceeds integer.max value, integer.max value is returned
     */
    int size();
    /**
     * Determine whether the element in the set is empty
     */
    boolean isEmpty();
    /**
     * Determine whether the specified element is included in the collection
     */
    boolean contains(Object o);
    /**
     * Returns an iterator. The order of elements in the iterator is not guaranteed unless the collection implementation class itself provides the guarantee
     */
    Iterator<E> iterator();
    /**
     * Converts a collection to an array in which the elements are in the same order as in the collection's production iterator
    *This method is a bridge between arrays and lists
     */
    Object[] toArray();
    /**
     * Returns an array of collection element types. If the collection satisfies the specified array and has enough space, the collection is returned in it
     *Otherwise, a new array of this collection size is returned.
     *If the collection is ordered, an array of traversal orders of this collection iterator is returned
     *If the array size is larger than the collection element, set to null at the end after the array satisfies the collection element
     *If the type of the element in the array is not a superclass of the element type in the collection, throw the ArrayStoreException exception exception
     *Throw NullPointerException if the specified array is empty
     */
    <T> T[] toArray(T[] a);

 

Modification:

/**
     * Make sure that this collection contains a specific element type. Returns true if the collection successfully adds elements. Returns false if the collection does not allow duplicate elements and already contains passed parameters
     * Collections that support this operation may limit which elements are added to the collection. In particular, some collections reject null elements, and some impose restrictions on the elements to be added.
     * Collection The implementation class should clearly indicate all the limitations in the documentation.
     * If the collection refuses to add a specific element for any reason other than the one already contained, an exception must be thrown instead of returning false. This preserves the invariance that the collection always contains the specified element after the call returns.
     */
    boolean add(E e);

    /**
     * If the collection contains this element, remove the element; if the collection contains more than one such element, remove the element satisfying (o==null?e==null:o.equals(e));
     * Returns true if the collection changes due to the call of the method; throws ClassCastException exception exception if the type of the specified element is incompatible with the collection (optional restriction);
     * If the specified element is null and the collection does not allow null elements, a NullPointerException exception (optional restriction) is thrown
     * Throw an unsupported operationexception if the collection does not support the remove operation (optional restriction)
     */
    boolean remove(Object o);

 

Batch operation:

/**
     * Returns true if this collection contains all elements of the specified collection
     * Throw ClassCastException exception (optional restriction) if the type of element in this collection is incompatible with this collection
     * Throw a NullPointerException exception (optional restriction) if one or more nulls exist in the element in the collection and the null element is not allowed in this collection
     */
    boolean containsAll(Collection<?> c);

    /**
     * Adds all elements of the specified collection to the this collection (optional action). If the specified collection is modified while the operation is in progress, the operation behavior is undefined.
     * (This means that if the specified set is this set and the set is non empty, then the behavior of the call is undefined. )If the result of the call changes the this set returns true if
     * addAll If the operation is not supported by this collection, an unsupported opertaionexception exception will be thrown
     * A NullPointerException exception is thrown if the specified collection contains empty elements and this collection does not allow empty elements
     * If this collection prevents the addition of the hidden collection element type, a ClassCastException exception will be thrown
     * Throw IllegalArgumentException if some properties of the elements of the specified collection prevent them from being added to this collection
     * Due to insertion restrictions, if not all elements can be added at this time, an IllegalStateException exception is thrown
     */
    boolean addAll(Collection<? extends E> c);

    /**
     * Remove all elements contained in the specified collection from this collection (optional action). After calling this function, the collection and the specified collection will no longer contain the same elements. Returns true if the collection changes due to a call
     * If the collection does not support the removeAll method, an unsupported operationexception is thrown
     * If the type of one or more elements in the collection is not compatible with the specified collection, ClassCastException is thrown (optional operation)
     * A NullPointerException exception is thrown if the collection contains one or more empty elements and the specified collection does not support empty elements (optional), or if the specified collection is empty
     */
    boolean removeAll(Collection<?> c);

    /**
     * Remove all elements in this collection that match the given Predicate. Errors or runtime exceptions raised during an iteration or by Predicate are forwarded to the caller
     * The default implementation uses its iterator to traverse all elements of the collection. Each matching element is removed using iterator.remove().
     * Unsupported operationexception will be thrown if the iterator of the collection does not support removal
     * Null pointerexception is thrown when the specified filter is empty. If the removal operation is not supported, unsupported operationexception is thrown
     */
    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;
    }

    /**
     * Keep only the elements contained in this collection in the specified collection (optional action), that is, remove all elements in this collection that are not contained in the specified collection. Returns true if the collection changes
     * If this collection does not support retainAll, an unsupported operationexception is thrown
     * If the type of one or more elements in the collection is not compatible with the specified collection, ClassCastException is thrown (optional operation)
     * Throw NullPointerException (optional operation) if the collection contains one or more empty elements and the specified collection does not support empty elements, or if the specified collection is empty
     */
    boolean retainAll(Collection<?> c);

    /**
     * Remove all elements in this collection. After this method call, the collection should be empty. If the collection clear operation is not supported, an UnsupportOperationException will be thrown.
     */
    void clear();

 

Compare and hash:

    boolean equals(Object o);

    int hashCode();

 

Default method:

/**
     * Default method, used in multithreading, not discussed for now
     */
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, 0);
    }

    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }

    default Stream<E> parallelStream() {
        return StreamSupport.stream(spliterator(), true);
    }

Reference resources: https://www.jianshu.com/p/ef2990140d05

Keywords: Java JDK

Added by williamZanelli on Tue, 14 Jan 2020 10:54:25 +0200