Java Collections Framework学习笔记之List接口

xiaoxiao2021-02-28  38

Java Collections Framework学习笔记之List

文章中使用的图均源: http://www.codejava.net

下图是List API的整体层次图

我们先看一下文档中对List的描述:

/** * An ordered collection (also known as a <i>sequence</i>). 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.<p> List是有序集合。 这个接口的使用者对表中每个元素的插入位置有着精确的控制。使用者可以通过索引访问元素,也可以在表中搜索元素。 * * Unlike sets, lists typically allow duplicate elements.<p> List允许重复的元素。 * * The <tt>List</tt> interface places additional stipulations, beyond those * specified in the <tt>Collection</tt> interface, on the contracts of the * <tt>iterator</tt>, <tt>add</tt>, <tt>remove</tt>, <tt>equals</tt>, and * <tt>hashCode</tt> methods. Declarations for other inherited methods are * also included here for convenience.<p> 除了Collection接口指定的iterator, add, remove, equals, 和hashCode方法外,List接口增加了一些规定。 * * The <tt>List</tt> interface provides four methods for positional (indexed) * access to list elements. Lists (like Java arrays) are zero based. Note * that these operations may execute in time proportional to the index value * for some implementations (the <tt>LinkedList</tt> class, for * example). Thus, iterating over the elements in a list is typically * preferable to indexing through it if the caller does not know the * implementation.<p> List接口提供了四个方法,方便根据索引访问表中元素。 注意,对于某些实现,这些操作可能在时间上与指数值成正比。因此,遍历表中的元素时,如果调用者不知道是哪种实现,最好根据索引遍历。 * * The <tt>List</tt> interface provides a special iterator, called a * <tt>ListIterator</tt>, that allows element insertion and replacement, and * bidirectional access in addition to the normal operations that the * <tt>Iterator</tt> interface provides. A method is provided to obtain a * list iterator that starts at a specified position in the list.<p> List接口提供特殊的迭代器,叫做ListIterator,这个迭代器除了提供Iterator接口提供的操作外,还允许元素的插入和替换,以及双向访问。提供了一种方法来获取从表中的指定位置开始的表的迭代器。 * * The <tt>List</tt> interface provides two methods to search for a specified * object. From a performance standpoint, these methods should be used with * caution. In many implementations they will perform costly linear * searches.<p> List接口提供了两个搜索特定对象的方法。从性能的立场看,应该小心使用这些方法。在一些实现中他们都是花费线性时间的搜索。 * * The <tt>List</tt> interface provides two methods to efficiently insert and * remove multiple elements at an arbitrary point in the list.<p> List接口提供两个在表的任意点有效插入和移除多个元素的方法。 * * Note: While it is permissible for lists to contain themselves as elements, * extreme caution is advised: the <tt>equals</tt> and <tt>hashCode</tt> * methods are no longer well defined on such a list. 注意:虽然允许list将自己作为元素,但是应小心的使用:在这样的list中,equals()和hashCode()方法将无法被明确定义。 */

总结下来List的特点就是: - 有序 - 可重复 - 可搜索 - 可同时操作多个元素 - 可将自己作为自己的元素

List API:

List是各种表类型的基础接口。它定义了表类型一般操作。抽象子类:AbstractList 和 AbstractSequentialList具体实现类:ArrayList, Vector, LinkedList 和 CopyOnWriteArrayList(这个类在java.util.concurrent包中)旧版集合:Vector在JDK中不在Java集合框架里的实现类 AttributeList, RoleList, RoleUnresolvedList 和 Stack.
List接口中的一些主要方法

java.util.List
继承了Collection接口 public interface List<E> extends Collection<E> { // Positional Access Operations // get和set使得用户可以访问或改变某指定位置上的项 E get(int index); E set(int index, E element); // 在指定位置添加一个新的项 void add(int index, E element); // 删除指定位置上的项 E remove(int index); }

List是所有表类型的基础接口,ArrayList和Linkedlist类是List的两个常见的实现。

这里简单介绍一下ArrayList和LinkedList类 - ArrayList:在后备数组中存储元素的实现。数组的大小会随着新元素的增加而扩展。当然也可以在创建一个新ArrayList时通过指定初始容量来设置默认大小(使用ensureCapacity方法),再有,trimToSize可以在ArrayList添加操作完成后使用,来避免浪费空间。基本上,ArrayList的以下几种操作都是常数时间:size, isEmpty, get, set, iterator, listIterator。因此,这种实现适合于在我们需要快速、随机访问元素时使用。

LinkedList:在双链表数据结构中存储元素的实现。在末端add/remove元素是常数时间;在表中其他位置的操作是线性时间。因此,如果我们需要在表的末端快速add/remove元素,可以考虑使用LinkedList。

Besides ArrayList and LinkedList,Vector class is a legacy collection and later was retrofitted to implement the List interface. Vector is thread-safe, but ArrayList and LinkedList are not. The following class diagram depicts the inheritance tree of the List collections: 除了ArrayList和LinkedList类,Vector类是一个旧版的集合,后来进行了改进以实现List接口。Vector是线程安全的,但是ArrayList和LinkedList不是线程安全的。

List接口的结构

转载请注明原文地址: https://www.6miu.com/read-2625998.html

最新回复(0)