Java 集合框架个人总结

Java 集合框架个人总结

个人能力有限,若有差错还请指正与交流:

这里写图片描述


1、ArrayList底层实现就是动态数组,初始大小为10,需要扩容时,size变为原length的1.5倍。如下java1.7版本源码:

public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

  public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }
发生扩容时,采用copy数组的方式,此时的新增才效率慢,未发生扩容时新增只是在数组最后追加赋值。所以arrayList可以重复有序。
删除效率低,因为每次都要未删除的移动所有元素

2、HashMap 底层实现是一个Entry数组,初始化table大小是16的Entry数组。每个数组元素是一个linkedList。当发生key的hash值碰撞时,如果key相同,则替换原来的value,否则在当前数组下标处链表中追加一个Entrty。

3、HashSet 底层实现是一个HashMap,每个key对应的value相同,一个Object。故hashset不能重复且无序。

    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();

    /**
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
     * default initial capacity (16) and load factor (0.75).
     */
    public HashSet() {
        map = new HashMap<>();
    }

[1]: Hashmap原理 http://www.importnew.com/10620.html
[2]: ArrayList原理 http://www.cnblogs.com/ITtangtang/p/3948555.html
[3]: Java transient关键字 http://www.cnblogs.com/lanxuezaipiao/p/3369962.html
[4]: 框架介绍 # 1) http://blog.csdn.net/u011487470/article/details/42420639
# # # # # # # # 2)http://doc.okbase.net/DavidIsOK/archive/94766.html
[5]迭代器 for循环 http://blog.csdn.net/zxq1138634642/article/details/8092327
[6]对象数组或list排序及Collections排序原理 http://trinea.iteye.com/blog/1248517
[7]TreeSet()详解http://www.cnblogs.com/ningvsban/archive/2013/05/06/3062535.html
[8]HashSet,TreeSet和LinkedHashSet的区别 http://www.cnblogs.com/Terry-greener/archive/2011/12/02/2271707.html
[9]Java1.8集合框架(Java Collections Framework)类图整理http://www.cnblogs.com/whatHowWhy/p/5852819.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值