一、论断

1. set.contains("obj")的效率显著高于list.contains("obj")

次要比拟了 ArrayList,LinkedListHashSet

2. 次要起因是 List 底层是通过遍历的形式去作比拟,而 Set 是算key的hash值的模式与汇合内元素比拟

二、源码

1. ArrayList

ArrayList<Object> list = new ArrayList<>();list.contains("");// 调用indexof    /**     * Returns true if this list contains the specified element.      * More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).     * 参数: o – element whose presence in this list is to be tested     * 返回: true if this list contains the specified element     */public boolean contains(Object o) {    return indexOf(o) >= 0;}/** * Returns the index of the first occurrence of the specified element in this list,  * or -1 if this list does not contain the element.  * More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), * or -1 if there is no such index. */public int indexOf(Object o) {    if (o == null) {        for (int i = 0; i < size; i++)            if (elementData[i]==null)                return i;    } else {        for (int i = 0; i < size; i++)            if (o.equals(elementData[i]))                return i;    }    return -1;}

2. LinkedList

// 相似 ArrayListLinkedList<Object> linkedList = new LinkedList<>();linkedList.contains("");public boolean contains(Object o) {    return indexOf(o) != -1;}public int indexOf(Object o) {    int index = 0;    if (o == null) {        for (Node<E> x = first; x != null; x = x.next) {            if (x.item == null)                return index;            index++;        }    } else {        for (Node<E> x = first; x != null; x = x.next) {            if (o.equals(x.item))                return index;            index++;        }    }    return -1;}

3. HashSet

HashSet<Object> set = new HashSet<>();set.contains("");/** * Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)). * 参数: o – element whose presence in this set is to be tested * 返回: true if this set contains the specified element */public boolean contains(Object o) {    return map.containsKey(o);}// 上面就是高效率的关键点,算hash二不是一个个比对/** * Returns true if this map contains a mapping for the specified key. * 参数: key – The key whose presence in this map is to be tested * 返回: true if this map contains a mapping for the specified key. */public boolean containsKey(Object key) {    return getNode(hash(key), key) != null;}/** * 计算key.hashCode()并将哈希值的高位扩大为低位。 */static final int hash(Object key) {    int h;    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);}/** * Implements Map.get and related methods * 先算比对hash 再 equals * 参数: hash – hash for key ,key – the key * 返回: the node, or null if none */final Node<K,V> getNode(int hash, Object key) {    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;    if ((tab = table) != null && (n = tab.length) > 0 &&        (first = tab[(n - 1) & hash]) != null) {        if (first.hash == hash && // always check first node            ((k = first.key) == key || (key != null && key.equals(k))))            return first;        if ((e = first.next) != null) {            if (first instanceof TreeNode)                return ((TreeNode<K,V>)first).getTreeNode(hash, key);            do {                if (e.hash == hash &&                    ((k = e.key) == key || (key != null && key.equals(k))))                    return e;            } while ((e = e.next) != null);        }    }    return null;}