• 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. 底层二叉树。
  • 相关阅读:
    Python 代码混淆工具概述
    Docker(1)
    ffmpeg源码笔记-AvFrame和AvPacket(四)
    云扩RPA研习社 | 解析流程开发主要步骤
    [MoeCTF 2023]——Web方向详细Write up、Re、Misc、Crypto部分Writeup
    用手机浏览器浏览不良网站,清理数据还有用吗?
    机器人路径规划:基于A*算法的机器人路径规划(提供Python代码)
    JavaScript的forEach循环和作用域
    优维EasyOps®全平台又一波新功能上线,操作体验更带劲
    运筹学基础【三】 之 决策
  • 原文地址:https://blog.csdn.net/qq_37400096/article/details/127457584