JAVA集合体系详解

来源:互联网 发布:淘宝手机回收的的钱 编辑:程序博客网 时间:2024/05/29 11:20

因为不同的需求,Java提供了各种集合类,虽然它们的数据结构不同,但是它们却有一些共性内容<比如:存储,获取,删除,判断等。>,通过不断的向上抽取,我们就能够得到一个集合的继承体系结构图。

一、Collection

Collection集合是单列集合的顶层根接口,因此在Collection中定义了所有单列集合通用的一些方法。它有两个子接口,分别是List和Set。

1. List接口

List接口的特点是元素有序元素可重复,它可以对列表中每个元素的插入位置进行精确地控制,用户可以根据元素的整数索引来访问、搜索列表中的元素。

Vector

Vector集合是JDK在1.0版本提供的线程安全的List接口实现类,底层通过Object数组实现,它有四个构造方法 :

① 无参构造:初始化长度为0,在无参构造方法中会调用int类型的带参构造创建一个长度为10的Object类型数组,当这个数组存储满后会创建这个原数组长度2倍的新数组以扩容。

    /**     * Constructs an empty vector so that its internal data array     * has size {@code 10} and its standard capacity increment is     * zero.     */    public Vector() {        this(10);    }

② 一个int类型的带参构造:在内部创建一个指定大小长度的Object类型数组用于存储数据,当这个数组存储满后会创建这个原数组长度2倍的新数组以扩容。

    /**     * Constructs an empty vector with the specified initial capacity and     * with its capacity increment equal to zero.     *     * @param   initialCapacity   the initial capacity of the vector     * @throws IllegalArgumentException if the specified initial capacity     *         is negative     */    public Vector(int initialCapacity) {        this(initialCapacity, 0);    }

③ 两个int类型的带参构造:在内部创建一个initialCapacity长度的Object类型数组用于存储数据,当这个数组存储满后,如果capacityIncrement > 0,则会创建这个原数组长度加上capacityIncrement 的新数组以扩容,否则会创建这个原数组长度2倍的新数组以扩容。

    /**     * Constructs an empty vector with the specified initial capacity and     * capacity increment.     *     * @param   initialCapacity     the initial capacity of the vector     * @param   capacityIncrement   the amount by which the capacity is     *                              increased when the vector overflows     * @throws IllegalArgumentException if the specified initial capacity     *         is negative     */    public Vector(int initialCapacity, int capacityIncrement) {        super();        if (initialCapacity < 0)            throw new IllegalArgumentException("Illegal Capacity: "+                                               initialCapacity);        this.elementData = new Object[initialCapacity];        this.capacityIncrement = capacityIncrement;    }

④ 一个Collection类型的带参构造:根据给定的Collection集合实现类,创建一个Vector,当这个数组存储满后,会创建这个原数组长度2倍的新数组以扩容。

    /**     * Constructs a vector containing the elements of the specified     * collection, in the order they are returned by the collection's     * iterator.     *     * @param c the collection whose elements are to be placed into this     *       vector     * @throws NullPointerException if the specified collection is null     * @since   1.2     */    public Vector(Collection<? extends E> c) {        elementData = c.toArray();        elementCount = elementData.length;        // c.toArray might (incorrectly) not return Object[] (see 6260652)        if (elementData.getClass() != Object[].class)            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);    }

ArrayList

ArrayList集合是JDK在1.2版本的时候提供的List接口实现类,底层也是通过Object数组实现,主要用于单线程访问场景,用来解决Vector由于线程同步带来的效率相对低下问题。它有三个构造方法 :

① 无参构造:初始化长度为0,在第一次存储元素的时候(add方法)会创建一个长度为10的新数组(类型为Object),当这个数组存储满后会创建这个原数组长度3/2的新数组以扩容。

    /**     * Constructs an empty list with an initial capacity of ten.     */    public ArrayList() {        super();        this.elementData = EMPTY_ELEMENTDATA;    }

② 一个int类型的带参构造:在内部创建一个指定大小长度的数组用于存储数据,扩容同上。

    /**     * Constructs an empty list with the specified initial capacity.     *     * @param  initialCapacity  the initial capacity of the list     * @throws IllegalArgumentException if the specified initial capacity     *         is negative     */    public ArrayList(int initialCapacity) {        super();        if (initialCapacity < 0)            throw new IllegalArgumentException("Illegal Capacity: "+                                               initialCapacity);        this.elementData = new Object[initialCapacity];    }

③ 一个Collection类型的带参构造:根据给定的Collection集合实现类,创建一个ArrayList,扩容同上。

    /**     * Constructs a list containing the elements of the specified     * collection, in the order they are returned by the collection's     * iterator.     *     * @param c the collection whose elements are to be placed into this list     * @throws NullPointerException if the specified collection is null     */    public ArrayList(Collection<? extends E> c) {        elementData = c.toArray();        size = elementData.length;        // c.toArray might (incorrectly) not return Object[] (see 6260652)        if (elementData.getClass() != Object[].class)            elementData = Arrays.copyOf(elementData, size, Object[].class);    }

LinkedList

LinkedList集合也是JDK在1.2版本的时候提供的List接口实现类,底层通过双向循环链表数据结构实现数据存储,初始长度为0,即只有一个表头节点,前节点和后节点信息均为null,用于表示一个空链表,没有扩容因子,具有增删快,查询慢且非线程安全的特点。内部通过一个Node实体类来实现链表式的数据存储,取出元素的时候,如果角标index > size/2,那么就从后向前依次遍历LinkedList所有元素,反之如果角标index < size/2,则从开头依次遍历所有元素,直到index位置元素被遍历到并返回,这也是查询慢的原因。有两个构造方法 :

① 无参构造:初始化长度为0

     /**     * Constructs an empty list.     */    public LinkedList() {}

② 一个Collection类型的带参构造:根据给定的Collection集合实现类,创建一个LinkedList。

     /**     * Constructs a list containing the elements of the specified     * collection, in the order they are returned by the collection's     * iterator.     *     * @param  c the collection whose elements are to be placed into this list     * @throws NullPointerException if the specified collection is null     */    public LinkedList(Collection<? extends E> c) {        this();        addAll(c);    }

④⑤⑥⑦⑧⑨⑩

快捷键

  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y

Markdown及扩展

Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档,然后转换成格式丰富的HTML页面。 —— [ 维基百科 ]

使用简单的符号标识不同的标题,将某些文字标记为粗体或者斜体,创建一个链接等,详细语法参考帮助?。

本编辑器支持 Markdown Extra ,  扩展了很多好用的功能。具体请参考Github.

表格

Markdown Extra 表格语法:

项目 价格 Computer $1600 Phone $12 Pipe $1

可以使用冒号来定义对齐方式:

项目 价格 数量 Computer 1600 元 5 Phone 12 元 12 Pipe 1 元 234

定义列表

Markdown Extra 定义列表语法:
项目1
项目2
定义 A
定义 B
项目3
定义 C

定义 D

定义D内容

代码块

代码块语法遵循标准markdown代码,例如:

@requires_authorizationdef somefunc(param1='', param2=0):    '''A docstring'''    if param1 > param2: # interesting        print 'Greater'    return (param2 - param1 + 1) or Noneclass SomeClass:    pass>>> message = '''interpreter... prompt'''

脚注

生成一个脚注1.

目录

[TOC]来生成目录:

    • 一Collection
      • List接口
        • Vector
        • ArrayList
        • LinkedList
    • 快捷键
    • Markdown及扩展
      • 表格
      • 定义列表
      • 代码块
      • 脚注
      • 目录
      • 数学公式
      • UML 图
    • 离线写博客
    • 浏览器兼容

数学公式

使用MathJax渲染LaTex 数学公式,详见math.stackexchange.com.

  • 行内公式,数学公式为:Γ(n)=(n1)!nN
  • 块级公式:

x=b±b24ac2a

更多LaTex语法请参考 这儿.

UML 图:

可以渲染序列图:

Created with Raphaël 2.1.0张三张三李四李四嘿,小四儿, 写博客了没?李四愣了一下,说:忙得吐血,哪有时间写。

或者流程图:

Created with Raphaël 2.1.0开始我的操作确认?结束yesno
  • 关于 序列图 语法,参考 这儿,
  • 关于 流程图 语法,参考 这儿.

离线写博客

即使用户在没有网络的情况下,也可以通过本编辑器离线写博客(直接在曾经使用过的浏览器中输入write.blog.csdn.net/mdeditor即可。Markdown编辑器使用浏览器离线存储将内容保存在本地。

用户写博客的过程中,内容实时保存在浏览器缓存中,在用户关闭浏览器或者其它异常情况下,内容不会丢失。用户再次打开浏览器时,会显示上次用户正在编辑的没有发表的内容。

博客发表后,本地缓存将被删除。 

用户可以选择 把正在写的博客保存到服务器草稿箱,即使换浏览器或者清除缓存,内容也不会丢失。

注意:虽然浏览器存储大部分时候都比较可靠,但为了您的数据安全,在联网后,请务必及时发表或者保存到服务器草稿箱

浏览器兼容

  1. 目前,本编辑器对Chrome浏览器支持最为完整。建议大家使用较新版本的Chrome。
  2. IE9以下不支持
  3. IE9,10,11存在以下问题
    1. 不支持离线功能
    2. IE9不支持文件导入导出
    3. IE10不支持拖拽文件导入


  1. 这里是 脚注内容. ↩
原创粉丝点击