Java容器类框架概述(0)

Java容器类概述数组

Java容器有两类框架,一类是Collection,一类是Map,下面经过两张图片来分析一下这两种容器,之因此说是容器,是由于Map不属于Collection,而是一个单独的接口框架

Collection接口

Collection接口
Collection接口

思惟导图中的标注黑体的是比较常见的集合,主要用Arraylist,LinkedList,HashSet,Collection继承了Iterable接口ide

Collection的内部方法

Collection继承关系
Collection继承关系

Collection的内部方法
Collection的内部方法

这些方法都很常见,根据名字基本上都能知道具体的做用,因此但凡是实现了Collection的接口都可以使用这些方法。工具

Collection的实现类

List

源码的注释this

  • An ordered collection (also known as a sequence). The user of this
    interface has precise control over where in the list each element is
    inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
  • 意思是说List是一个有序的Collection,这个接口的使用者可以准确的控制他所插入的每个元素,使用者也可以根据他们的整数索引在List中查找元素。

常见的List的实现有ArrayList,LinkedList设计

Set

源码的注释3d

  • A collection that contains no duplicate elements. More formally, sets
    contain no pair of elements e1 and e2 such that
    e1.equals(e2), and at most one null element. As implied by
    its name, this interface models the mathematical set abstraction.
  • 一个没有重复元素的集合,而且,set也不能存放两个用equals方法比较相等的元素,最多能够放入一个null值。就像他的名字同样,这个接口模拟了数学中的集合。

常见的set集合有HastSetrest

Queue

源码的注释code

  • A collection designed for holding elements prior to processing.
    Besides basic operations,queues provide additional insertion, extraction, and inspection
    operations. Each of these methods exists in two forms: one throws
    an exception if the operation fails, the other returns a special
    value (either {@code null} or {@code false}, depending on the
    operation). The latter form of the insert operation is designed
    specifically for use with capacity-restricted {@code Queue}
    implementations; in most implementations, insert operations cannot
    fail.
  • 一种被设计用来能够放置优先级元素的集合,除了基本的Collection操做之外,queue还提供另外的插入,提取和检查操做。每个方法的执行结构存在两种形式:一种是执行失败会跑出一种异常,另一种是返回特定的结果:null值或者false,具体取决于操做的结果。后一种插入操做的返回形式是针对特定容量的实现,在queue的大多实现里,插入操做是不会失败的。

常见的queue的实现有BlockingQueue跟PriorityQueueorm

Comparable跟Comparator

这是Java在集合中提供的两个接口,主要用来比较两个元素和进行排序,若是只是比较相同的两个类,则均可以实现,不过仍是有些区别:

  • Comparator是在类的外部进行排序,Comparable是在类的内部进行排序
  • Comparator比较适合对于多个类进行排序,只须要实现一个Comparator就能够,Comparable则须要在每一个类中实现Comparable接口

Collection的工具类

Collection提供了两个工具类,一个是Collections,一个是Arrays

Collections

Collections提供的方法
Collections提供的方法

Arrays

Arrays提供的方法
Arrays提供的方法

Java源码的命名都比较规范,经过名字基本上能看出用法,有些即时不可以看出来用法,点击去看一下注释,或者写个小demo也能知道,经过这两个工具类,实际上能够简化咱们队集合跟数组的操做,例如排序,同步,求最大值,最小值等,感兴趣的能够看一下,文章图片比较多,是由于以为一图胜千言,因此代码就尽可能少贴喽。

Map接口

Map接口
Map接口

思惟导图中的标注黑体的是比较常见的集合,主要有HashedMap,LinkedHashMap,TreeMap。

Map的内部方法

Map的内部方法
Map的内部方法

源码注释:

  • An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
  • 一个可以将key应射成value的Objec,Map不能有重复的key,每一个key最多只能对应一个value

Map的实现类

比较常见的是HashMap,LinkedHashMap

总结

上面大题就是Java的整个容器框架,分析地比较简单,接下来主要是分析下常见的Java容器类的实现类,毕竟整个源码比较复杂,不能面面俱到。主要是经过思惟导图和IDEA生成的UML来进行分析,这样会显得整个思路比较清晰,不至于一头钻进源码,质检数目,不见森林。

相关文章
相关标签/搜索