Collection Source Reading Notes

introduction

Map Collection interface and the interface is the root interface Java Collections Framework, List, Queue, Set and other interfaces to achieve the Collection interface, which allows the collection of some repetitive elements, and some collection allows orderly. It is precisely because we need to use a collection of different characteristics, so jdk does not provide direct realization of the Collection interface, but above those more specific interfaces to inherit the Collection interface, let the concrete implementation class to implement these interfaces. In short, Collection interface is simply extracted from the set of all types out of the common, and List, Queue, Set the interface is from the collection of a certain type (ordered or disordered, can not be repeated or severe) extracted in common, like list interface is designed specifically for this collection list extracted in common.

Collection is a model oriented programming interface, you can switch between multiple implementation classes through it, and this is one of the charm of the object-oriented programming.

First, the interface definition

public interface Collection<E> extends Iterable<E>

Directly inherited from the Collection interface Iterable interface, so all collections Collection supports for-each loop.

Second, the method definition

For collections, we desired nothing more than some of the operations: counting the number of elements, the elements change search deletions, determines the presence of an element, empty, etc., these common operations Collection extraction package.

(A) query method

(1)

int size();

Returns the number of elements in the set, if the number is greater than Integer.MAX_VALUE, returns Integer.MAX_VALUE

(2)

boolean isEmpty();

Determining whether the set is empty, i.e., whether the number of elements is 0, returns true if it is empty, otherwise false

(3)

boolean contains(Object o);

Determining whether the collection contains the specified element (using equals () method)

When set such that at least one element e (o == null e == null:? O.equals (e)) is satisfied, returns true
When the specified type is not compatible with the element of the set, throws a ClassCastException;
When the specified element is null and this collection does not permit null elements, throws NullPointerException
(4)
Iterator<E> iterator();

Iteration may return an iterator over a set

(5)

Object[] toArray();

Returns a collection of all the elements of the array contains

The returned array is newly allocated, the collection did not keep a reference to the array, so the caller of the method can freely, safely modify the array

<T> T[] toArray(T[] a);

Like the above also returns an array containing all the elements of a collection

However, the type of the array is specified: the same type of operation and type of the specified array is returned when running the array. If the size of the specified array can hold all the elements of the collection, the return still returns a collection of all the elements contained in the specified array (when the size of the array is greater than the specified number of elements of the set, all the elements of the array after the table size are set assigned to null); otherwise, assigning a run when the specified array has a type, and size of the new array in this collection.

That is, this method can be used to precisely control the return type of the array, and the array of the specified size distribution.

When the specified array is null, a NullPointerException is thrown;

When running the specified array type is not set when the runtime type of each element of the parent class, is thrown ArrayStoreException

toArray (new Object [0]) and is equivalent to toArray () method, when we want to return a String array collection x, just

String[] y = x.toArray(new String[0]);

Two toArray () method acts as a bridge between arrays and collections, and we convert all types of collections, but also often use these two methods.

Second, modify operation

boolean add(E e);

Adds the specified element to the collection.

If added (collection after this method is called the end of the change), returns true; if the collection does not allow duplicate and there is already specified element, returns false

A collection of different types of elements are added may be limited, for example, refuse collection and some add null elements, and some collections on the type of additive elements made some restrictions. If there is any reason to refuse collection add elements (except in the case do not allow repetition of the collection has been specified element), you should throw an exception, rather than just return false.

When a certain type of collection does not support this method throws UnsupportedOperationException;

When the collection does not allow adding the specified data element type, throws a ClassCastException;

When the specified element is null set and null elements is not allowed, a NullPointerException is thrown;

When some of the parameters specified element prevents the element added to the collection, an IllegalArgumentException is thrown;

When inserted into the specified element because of some restrictions and can not be added to the collection, throws IllegalStateException

boolean remove(Object o);

Delete object o with a collection of elements are equal, i.e., deleting one or more collection satisfies (o == null e == null:? O.equals (e)) of the element e

If there is such an element and deleted successfully return true; false otherwise

When the specified element is null set and null elements is not allowed, a NullPointerException is thrown;

When the specified element type is not compatible with the set, dished out ClassCastException;

When the collection does not support this operation, throw an UnsupportedOperationException

Third, bulk operations

(1)

boolean containsAll(Collection<?> c);

Analyzing all the elements of the specified collection contains bound

If it does, then return true; false otherwise

When one or more elements of the specified collection set is not compatible with this, throw a ClassCastException;

When the specified set of one or more null element, and this does not allow the collection null elements, a NullPointerException is thrown

(2)

boolean addAll(Collection<? extends E> c);

Adds the specified collection of all the elements in this collection

If, after this method is called, the collection is changed, it returns true

Note, however, when the progress of this operation, the specified collection is modified, the behavior of this operation will be undefined. For example, when the designated collection itself is set, and this set is not empty.

When a certain type of collection does not support this method throws UnsupportedOperationException;

When the type of the data sets do not allow to add a specified collection element, throws a ClassCastException;

When an element is null and designated collection sets do not allow null elements, a NullPointerException is thrown;

When some of the parameters of a designated set of elements prevents the element added to the collection, an IllegalArgumentException is thrown;

When an element is inserted into the specified collection because some of the restrictions can not be added to the collection, throws IllegalStateException

(3)

boolean removeAll(Collection<?> c);

Delete collection contains the specified collection of all the elements

If this method is called after the collection has changed, return true

When a certain type of collection does not support this method throws UnsupportedOperationException;

When specifying a set of one or more of the elements of this collection are not compatible, throw a ClassCastException;

When one or more elements of the specified collection set is null and null elements is not allowed, a NullPointerException is thrown;

(4)

boolean retainAll(Collection<?> c);

Retaining only the set of all the elements contained in the specified set, the method and the opposite method of the above

If, after this method is called, the collection has changed, return true

When a certain type of collection does not support this method throws UnsupportedOperationException;

When specifying a set of one or more of the elements of this collection are not compatible, throw a ClassCastException;

When one or more elements of the specified collection set is null and null elements is not allowed, a NullPointerException is thrown;

(5)

void clear();

The collection is empty after deleting all of the elements in this collection, deleted successfully

When this collection does not support this method throws UnsupportedOperationException

Fourth, the comparison and hashing

 (1)

boolean equals(Object o);

Compare whether the specified object is equal to the set

This abstract method here is to ensure that all subclasses override this method to ensure the correct behavior of equals. As for what makes equal, depending on the specific implementation class.

When we write a class that implements the Collection interface, if no specific demand for equal judgment, the direct implementation of the Collection can be, because the default implementation class equals () method of the Object class inheritance (compare two objects are the same Object); If you need a "value comparison", then you need to override this method.

General rule equals Object class () method is, a.equals (b) if and only if b.equals (a), for the List, Set the equals (), there are: list only equal to the list, set and only equal to the set. Therefore, we custom equals () method when, when the set list and compare must return false. The same logic, we can not make a class while achieving Set and List interfaces.

(2)

int hashCode();

Returns the hash code value set.

Here the abstract of this method, the same is to ensure that all subclasses override this method to ensure the correct behavior of hashCode

Note: Any kind of rewriting Object equals () method, you must rewrite the Object hashCode () methods to meet the following general rules:

c1.equals (c2) means c1.hashCode () == c2.hashCode () have the same result

Five other methods

jdk 8 Collection also provides default implementations of some methods

(1)

    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;
    }

Delete the collection that match a given condition filter elements (access order in accordance with the iterator)

When one or more elements are removed, then return true; false otherwise

You can see, this method iterates over the set of all elements that match a given criteria for each element are removed (remove the iterator through when you delete () method deletes) from the collection

When a given filter is null, throw NullPointerException;

When the element can not be removed from the collection, throw UnsupportedException

(2)

    @Override
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, 0);
    }

Create a set of elements on the Spliterator, this method covers spliterator Iterable interface () method

Spliterator stream interface may be decomposed into a plurality of parts which can be processed in parallel. In actual use, we rarely used directly Spliterator, but to use it as a similar Iterator. For example, Spliterator <T> spliterator = list.spliterator ();

Collection interface also provides a stream (), parallelStream () method, which utilizes the method and obtained by calling Spliterator spliterator (), which contributes to two parallel processing method of data collection.

(3)

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

 In order to return a set flow as a source of

(4)

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

Returns a collection as to a possible source of parallel streams 

 

Guess you like

Origin www.cnblogs.com/JeremyChan/p/11125701.html
Recommended