大有作为的集合_List系列

大有可为的java集合

根据继承体系可以将集合分为两大类
一类实现了 Collection 接口 ,另一类实现了 Map 接口
下面简单介绍一下:

1.Collection 是所有集合类的根接口
2.Set 接口的实现类不允许重复的元素 Eg:HashSet、LinkedHashSet、TreeSet
1
3.List 接口的实现类允许重复元素,可通过 index 访问对应位置上的元素 Eg:LinkedList、ArrayList

1.HashMap 是最常用的Map,可以根据键直接获取对应的值,它根据键的 hashCode 值存储数据,所以访问速度非常快。HashMap 最多只允许一条记录的键为 null (多条会覆盖);但允许多条记录的值为null
2.TreeMap能够把它保存的记录根据键(不允许键的值为null)排序,默认是升序,也可以指定排序的比较器,当用迭代器(Iterator)遍历 TreeMap 时,得到的记录是排过序的。
3.Hashtable 的键和值均不允许为 null,是线程同步的,也就是说任一时刻只有一个线程能写 Hashtable,线程同步会消耗掉一些性能,因此 Hashtable 在写入时花费的时间也会比较多。
4.LinkedHashMap 保存了记录的插入顺序,当用迭代器(Iterator)遍历 LinkedHashMap 时,先得到的记录肯定是先插入的。键和值均允许为 null。

LinkedList 和 ArrayList 有什么区别啊?

LinkedList 其实是一个双向链表,源码如下:

public class LinkedList<E>
{
    transient int size = 0;

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }
} 



在这里插入代码片Z大萨达

1.LinkedList 包含一个非常重要的内部类——Node。Node 是节点所对应的数据结构,item 为当前节点的值,prev 为上一个节点,next 为下一个节点——这也正是“双向”链表的原因。first 为 LinkedList 的第一个节点,last 为最后一个节点。
2.size是 LinkedList 的节点个数。当往 LinkedList 添加一个元素时,size+1,删除一个元素时,size-1。

ArrayList其实是一个动态数组,源码如下:

public class ArrayList<E>
{
     /**
     * The array buffer into which the elements of the ArrayList are stored.
     * The capacity of the ArrayList is the length of this array buffer. Any
     * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
     * will be expanded to DEFAULT_CAPACITY when the first element is added.
     */
    transient Object[] elementData; // non-private to simplify nested class access

    /**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
    private int size;
}       

1.elementData 是 Object 类型的数组,用来保存添加到 ArrayList 中的元素。如果通过默认构造参数创建 ArrayList 对象时,elementData 的默认大小是 10。当 ArrayList 容量不足以容纳全部元素时,就会重新设置容量,新的容量 = 原始容量 + (原始容量 >> 1)(>>1就相当除2)。

private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    elementData = Arrays.copyOf(elementData, newCapacity);
}

2 .size 是 ArrayList 的元素个数。当往 ArrayList 添加一个元素时,size+1,删除一个元素时,size-1。
由于LinkedList ArrayList 底层实现的不同(一个双向链表,一个动态数组),它们之间的区别也很一目了然。

关键点LinkedList 在添加(add(E e))、插入(add(int index, E element))、删除(remove(int index))元素的性能上远超 ArrayList

原因ArrayList在添加、插入、删除元素的时候,会有意或者无意(扩容)的调用 System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 方法,该方法对性能的损耗是非常严重的。

LinkedList 不存在扩容的问题,也不需要对原有的元素进行复制;只需要改变节点的数据就好了。
看一下源码:

Node<E> node(int index) {
    // assert isElementIndex(index);

    if (index < (size >> 1)) {
        Node<E> x = first;
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {
        Node<E> x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}

观察 LinkedList 的源码,就能够发现, LinkedList 在定位 index 的时候会先判断位置(是在 1 / 2 的前面还是后面),再从前往后或者从后往前执行 for 循环依次找。
再来看 ArrayList 的相关源码:

@SuppressWarnings("unchecked")
E elementData(int index) {
    return (E) elementData[index];
}

ArrayList 直接根据 index 从数组中取出该位置上的元素,不需要 for 循环遍历啊——这样显然更快!

希望大家多多提出宝贵意见!下章Map见!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值