Arrays.asList的那点事

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class ArraysAsListDemo {
    public static void main(String[] args) {
        List<String> source1 = new ArrayList<String>(Arrays.asList("1", "2", "3"));
        ArrayList<String> source2 = new ArrayList<String>(Arrays.asList("5", "62", "3"));
        source2.removeAll(source1);
        System.out.println(source2);
//        Arrays.asList("1", "2", "3").remove("1");//会报错。因为Arrays$ArrayList中没有实现List接口中的remove方法

        System.out.println(source1.containsAll(source2));
        HashSet<String> total = new HashSet<String>();
        total.addAll(source1);
        total.addAll(source2);
        System.out.println(total.size() > (source1.size() + source2.size()));
        System.out.println(total.size() < (source1.size() + source2.size()));
    }

}



    // Misc

    /**
     * Returns a fixed-size list backed by the specified array.  (Changes to
     * the returned list "write through" to the array.)  This method acts
     * as bridge between array-based and collection-based APIs, in
     * combination with {@link Collection#toArray}.  The returned list is
     * serializable and implements {@link RandomAccess}.
     *
     * <p>This method also provides a convenient way to create a fixed-size
     * list initialized to contain several elements:
     * <pre>
     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
     * </pre>
     *
     * @param a the array by which the list will be backed
     * @return a list view of the specified array
     */
    public static <T> List<T> asList(T... a) {
    return new ArrayList<T>(a);
    }

    /**
     * @serial include
     */
    private static class ArrayList<E> extends AbstractList<E>
    implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
    private final E[] a;

    ArrayList(E[] array) {
            if (array==null)
                throw new NullPointerException();
        a = array;
    }

 说明:

在jdk文档中对RandomAccess接口的定义如下:
public interface RandomAccess
List 实现所使用的标记接口,用来表明其支持快速(通常是固定时间)随机访问。
此接口的主要目的是允许一般的算法更改其行为,从而在将其应用到随机或连续访问列表时能提供良好的性能。

将操作随机访问列表的最佳算法(如 ArrayList )应用到连续访问列表(如 LinkedList )时,可产生二次项的行为。
如果将某个算法应用到连续访问列表,那么在应用可能提供较差性能的算法前,鼓励使用一般的列表算法检查给定列表是否为此接口的一个 instanceof ,
如果需要保证可接受的性能,还可以更改其行为。现在已经认识到,随机和连续访问之间的区别通常是模糊的。
例如,如果列表很大时,某些 List 实现提供渐进的线性访问时间,但实际上是固定的访问时间。这样的 List 实现通常应该实现此接口。

强调:
JDK中推荐的是对List集合尽量要实现RandomAccess接口
如果集合类是RandomAccess的实现,则尽量用for(int i = 0; i < size; i++) 来遍历而不要用Iterator迭代器来遍历,在效率上要差一些。
反过来,如果List是Sequence List,则最好用迭代器来进行迭代。

package java.util;
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    private static final long serialVersionUID = 8683452581122892189L;

    /**
     * The array buffer into which the elements of the ArrayList are stored.
     * The capacity of the ArrayList is the length of this array buffer.
     */
    private transient Object[] elementData;

    /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    private int size;
public interface List<E> extends Collection<E> {
    // Query Operations

    /**
     * Returns the number of elements in this list.  If this list contains
     * more than <tt>Integer.MAX_VALUE</tt> elements, returns
     * <tt>Integer.MAX_VALUE</tt>.
     *
     * @return the number of elements in this list
     */
    int size();

    /**
     * Returns <tt>true</tt> if this list contains no elements.
     *
     * @return <tt>true</tt> if this list contains no elements
     */
    boolean isEmpty();
* @author  Josh Bloch
 * @author  Neal Gafter
 * @version 1.55, 04/21/06
 * @see        Set
 * @see        List
 * @see        Map
 * @see        SortedSet
 * @see        SortedMap
 * @see        HashSet
 * @see        TreeSet
 * @see        ArrayList
 * @see        LinkedList
 * @see        Vector
 * @see     Collections
 * @see        Arrays
 * @see        AbstractCollection
 * @since 1.2
 */

public interface Collection<E> extends 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
     * <tt>Integer.MAX_VALUE</tt>.
     *
     * @return the number of elements in this collection
     */
    int size();

    /**
     * Returns <tt>true</tt> if this collection contains no elements.
     *
     * @return <tt>true</tt> if this collection contains no elements
     */
    boolean isEmpty();





原文地址:https://www.cnblogs.com/softidea/p/4990463.html