Android: List\Set\Map 持有对象

看 thinking in java翻到到持有对象,大概意思就是一个容器保存(持有)对象,其中Collection\List\ArrayList等等不知所云,这里从源码梳理了下。

1] Iterator


libcore/ojluni/src/main/java/java/util/Iterator.java
/**
 * An iterator over a collection.  {Iterator} takes the place of
 * {Enumeration} in the Java Collections Framework.  Iterators
 * differ from enumerations in two ways:
 *    Iterators allow the caller to remove elements from the
 *           underlying collection during the iteration with well-defined
 *           semantics.
 *      Method names have been improved.
 */
public interface Iterator<E> {//泛型接口类(对象类型参数化)
    boolean hasNext();

    E next();

    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    //通配符?参数类型限定 super E
    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

/* 具体能干啥?
 * Represents an operation that accepts a single input argument and returns no
 * result. Unlike most other functional interfaces, {@code Consumer} is expected
 * to operate via side-effects.
 */
@FunctionalInterface
public interface Consumer<T> {

    //Performs this operation on the given argument.
    void accept(T t);

    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

2] collection


/**
 * The root interface in the <i>collection hierarchy</i>.  A collection
 * represents a group of objects, known as its elements.  Some
 * collections allow duplicate elements and others do not.  Some are ordered
 * and others unordered.
 */
public interface Collection<E> extends Iterable<E> {
    // Query Operations
    int size();
    boolean isEmpty();
    boolean contains(Object o);

    Iterator<E> iterator();

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

    // Modification Operations
    boolean add(E e);
    boolean remove(Object o);

    // Bulk Operations
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c);
    boolean removeAll(Collection<?> c);
    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;
    }
    boolean retainAll(Collection<?> c);
    void clear();

    // Comparison and hashing
    boolean equals(Object o);
    int hashCode();
    @Override
    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);
    }
}

2.1] AbstractCollection -> 


public abstract Iterator<E> iterator();
public abstract int size();
/**提供实现 Collection的 AbstractCollection: 接口没有任何实现而abstract有基本实现
 * This class provides a skeletal implementation of the Collection
 * interface, to minimize the effort required to implement this interface.
 *
 * To implement an unmodifiable collection, the programmer needs only to
 * extend this class and provide implementations for the iterator and
 * size methods.  (The iterator returned by the <tt>iterator</tt>
 * method must implement hasNext and next.
 *
 * To implement a modifiable collection, the programmer must additionally
 * override this class's add method (which otherwise throws an
 * <tt>UnsupportedOperationException</tt>), and the iterator returned by the
 * <tt>iterator</tt> method must additionally implement its <tt>remove</tt>
 * method.
 */


3] List

其他set\ map和list类似也是有Abstract class等组成,list\set\map的root object都是collection
/*
 * An ordered collection (also known as a sequence).  The user of this
 * interface has precise control over where in the list each element is
 * inserted.  The user can access elements by their integer index (position in
 * the list), and search for elements in the list.
 */
List也是个Interface
public interface List<E> extends Collection<E> {}
又增加了...等接口
int indexOf(Object o);
E get(int index);

3.1] AbstractList


public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
/**
 * This class provides a skeletal implementation of the {@link List}
 * interface to minimize the effort required to implement this interface
 * backed by a "random access" data store (such as an array).  For sequential
 * access data (such as a linked list)
 */


3.2] ArrayList


public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
/**
 * Resizable-array implementation of the <tt>List</tt> interface.  Implements
 * all optional list operations, and permits all elements, including
 * <tt>null</tt>.  In addition to implementing the <tt>List</tt> interface,
 * this class provides methods to manipulate the size of the array that is
 * used internally to store the list.  (This class is roughly equivalent to
 * <tt>Vector</tt>, except that it is unsynchronized.)
 */


4] set


/**
 * A collection that contains no duplicate elements.  More formally, sets
 * contain no pair of elements e1 and e2 such that
 * e1.equals(e2), and at most one null element.  As implied by
 * its name, this interface models the mathematical set abstraction.
 */

5] Map


/**
 * An object that maps keys to values.  A map cannot contain duplicate keys;
 * each key can map to at most one value.
 */
    /**
     * A map entry (key-value pair).  The { Map.entrySet} method returns
     * a collection-view of the map, whose elements are of this class.  The
     * only way to obtain a reference to a map entry is from the
     * iterator of this collection-view.  These {Map.Entry} objects are
     * valid only for the duration of the iteration; more formally,
     * the behavior of a map entry is undefined if the backing map has been
     * modified after the entry was returned by the iterator, except through
     * the {setValue} operation on the map entry.
     *

     */
    interface Entry<K, V> {}

6] Arrays


// Arrays和ArrayList没有什么关系,ArrayList是用Array修饰List的,表示List顺序访问
public class Arrays {}
/**
 * This class contains various methods for manipulating arrays (such as
 * sorting and searching). This class also contains a static factory
 * that allows arrays to be viewed as lists.
 */

猜你喜欢

转载自blog.csdn.net/u011279649/article/details/88692442