• Java中集合知识点


    一、集合

    集合总主要是三种:List,Set,Queue

    1. Collection:是集合List,Set,Queue的基本的接口。
    2. Iterator:迭代器,提供方法访问集合中的数据。
    3. Map :是映射表的基础接口。

    二 、List集合

    ArrayList集合的特点:

    1. 排列有序,可重复
    ArrayList<String> list = new ArrayList<>();
            list.add("b");
            list.add("a");
            list.add("a");
            System.out.println(list.get(0));
            System.out.println(list.get(1));
            System.out.println(list.get(2));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 底层使用数组
        private static final Object[] EMPTY_ELEMENTDATA = {};
    
        /**
         * Shared empty array instance used for default sized empty instances. We
         * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
         * first element is added.
         */
        private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    
        /**
         * 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 速度快,增删慢

    2. 线程不安全

    3. 容量不够,ArrayList是当前容量*1.5+1

      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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Vector集合的特点:

    1. 排列有序,可重复
    2. 底层使用数组
    3. 速度快,增删慢
    4. 线程安全,效率低
    5. 容量不够,默认扩展一倍容量
       private void grow(int minCapacity) {
            // overflow-conscious code
            int oldCapacity = elementData.length;
            int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                             capacityIncrement : oldCapacity);
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            if (newCapacity - MAX_ARRAY_SIZE > 0)
                newCapacity = hugeCapacity(minCapacity);
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    LinkedList集合的特点

    1. 排列有序,可重复
    2. 底层使用双向循环表数据结构。
    3. 查询速度慢,增加和删除快
    4. 线程不安全。

    三、Set 集合

    HashSet集合的特点

    1. 排列无序,不可重复
    2. 底层使用hash表实现
    3. 存取速度快
    4. 内部是HashMap

    TreeSet

    1. 排列无序,不可重复
    2. 底层使用二叉树实现
    3. 排序存储
    4. 内部是TreeMap和sortedSet

    LinkedHashSet

    1. 采用hash表存储,并用双向链表记录插入顺序。
    2. 内部是LinkedHashMap。

    Queue

    在两端出入的List,可以使用数组和链表实现。

    四、Map集合

    1.HashMap集合的特点

    1. 键不可重复,值可以重复。
    2. 底层哈希表
    3. 线程不安全
    4. 允许key值为null,value也可以为空。
    5. capacity:当前数组容量,始终保持 2^n,可以扩容,扩容后数组大小为当前的 2 倍。
    6. loadFactor:负载因子,默认为 0.75。
    7. threshold:扩容的阈值,等于 capacity * loadFactor

    Hashtable集合的特点

    1. 键不可重复,值可重复
    2. 底层哈希表
    3. 线程安全
    4. key value都不允许为空

    TreeMap集合的特点

    1. 键不可重复,值可重复
    2. 底层二叉树。
  • 相关阅读:
    领域驱动设计(DDD)系列文章前言
    SpringCloud链路追踪SkyWalking-第三章-接入微服务
    【【RAM的verilog 代码 + testbench】】
    zabbix安装
    从数学老师转行到银行做开发,我都经历了什么……
    26. 图论 - 树
    Android Anr traces.txt 最全最完整说明文档
    目标检测网络系列——YOLOV3
    高空简易水果采摘装置设计(CAD+proe)
    数据治理浅析
  • 原文地址:https://blog.csdn.net/qq_37400096/article/details/127457584