原创

Java中的集合(概况)


接下来自己准备将JDK源码(JDK8)好好过一遍,基础还是挺重要的。

  • Java中的集合都在util包下,这是很正常的,集合嘛,当然属于工具里的东西咯。

  • 集合类存放的都是对象的引用,这很好理解,实际上存的是堆内存的地址。

  • 集合类型主要有3种:set(集)、list(列表)和map(映射)。

  • Set 和 List 的 最大父接口是 Collection,map的 最大父接口是Map

  • 下面是一张Collection接口简单的树状图

Collection(I)
|---List(I)
    |---ArrayList
    |---LinkedList
    |---Vector
|---Set(I)
    |---HashSet
    |---TreeSet
|---Queue(I)
  • 下面是一张Map接口简单的树状图

Map(I)
|---HashMap
    |---LinkedHashMap
|---Hashtable
|---TreeMap
|---ConcurrentHashMap
  • Collection 接口 介绍

我们打开源码:第一段:

/**
 * The root interface in the <i>collection hierarchy</i>.  A        collection
 * represents a group of objects, known as its <i>elements</i>.  Some
 * collections allow duplicate elements and others do not.  Some are ordered
 * and others unordered.  The JDK does not provide any <i>direct</i>
 * implementations of this interface: it provides implementations of more
 * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>.  This interface
 * is typically used to pass collections around and manipulate them where
 * maximum generality is desired.
  • 这里很明确说明了:

  1. Collection接口是Collection(Map,Set)集合层次结构中的根接口。

  2. Collection接口有些允许重复的元素有些不允许,有的元素有序有的元素无序。

  3. Collection接口不提供直接的实现类,他提供子接口例如List,Set

  4. Collection接口通过操作集合元素达到最大的通用性

接着第三段:

 * <p>All general-purpose <tt>Collection</tt> implementation classes (which
 * typically implement <tt>Collection</tt> indirectly through one of its
 * subinterfaces) should provide two "standard" constructors: a void (no
 * arguments) constructor, which creates an empty collection, and a
 * constructor with a single argument of type <tt>Collection</tt>, which
 * creates a new collection with the same elements as its argument.  In
 * effect, the latter constructor allows the user to copy any collection,
 * producing an equivalent collection of the desired implementation type.
 * There is no way to enforce this convention (as interfaces cannot contain
 * constructors) but all of the general-purpose <tt>Collection</tt>
 * implementations in the Java platform libraries comply.
  • 表达的意思大致为:

  1. 通过Collection子接口(例如Set,List)间接实现Collection接口的实现类应该提供一个无参的构造方法来创建一个空的集合,一个1个参数的构造方法来创建一个相同元素个数的集合。

  2. 1个参数的构造方法允许我们复制任何其他的集合生成所需实现类型的等效集合,但是由于接口不能被实例化,这个行不通。不过其他通用的集合在java平台遵守这个约定。

下面这一段:

 * <p>Many methods in Collections Framework interfaces are defined in
 * terms of the {@link Object#equals(Object) equals} method.  For example,
 * the specification for the {@link #contains(Object) contains(Object o)}
 * method says: "returns <tt>true</tt> if and only if this collection
 * contains at least one element <tt>e</tt> such that
 * <tt>(o==null ? e==null : o.equals(e))</tt>."  This specification should
 * <i>not</i> be construed to imply that invoking <tt>Collection.contains</tt>
 * with a non-null argument <tt>o</tt> will cause <tt>o.equals(e)</tt> to be
 * invoked for any element <tt>e</tt>.  Implementations are free to implement
 * optimizations whereby the <tt>equals</tt> invocation is avoided, for
 * example, by first comparing the hash codes of the two elements.  (The
 * {@link Object#hashCode()} specification guarantees that two objects with
 * unequal hash codes cannot be equal.)  More generally, implementations of
 * the various Collections Framework interfaces are free to take advantage of
 * the specified behavior of underlying {@link Object} methods wherever the
 * implementor deems it appropriate.
  • 说明了一点:

  1. Collection接口很多方法:例如:contains(),equals(),hashCode()和Object类的方法是一致的,因此实现类在有条件的情况下可以直接使用这些方法。

  • Collection接口方法一览

接口方法

  • 一眼看上去可以分为这么几块

  1. 判断: contains(),containsAll(),equals(),isEmpty()

  2. 添加: add(),addAll()

  3. 删除: remove(),removeAll(),clear(),removeIf()[JDK8新增],retainAll()

  4. 基础信息:size(),hashCode()

  5. 转化数组/流:(有些是JDK8新增的default方法): toArray(),stream(),iterator(),spliterator()

这里很清晰的定义了一些基本的接口抽象方法,常见的增删判断转化等等。

  • AbstractCollection是一个抽象类,实现了Collection接口一些最基本的通用操作,把复杂的和业务相关的延迟到子类实现。他主要实现了contains(), isEmpty(), toArray(), remove(), clear() 这几个操作。

数据结构
基础
javase
  • 作者:管理员(联系作者)
  • 发表时间:2020-03-19 09:53
  • 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
  • 公众号转载:请在文末添加作者公众号二维码
  • 微信公众号

    评论

    留言