Java List集合

我们先看一下jdk1.9对其的描述:

什么是List,也就是一个有序集合(序列)。

1.List接口

List集合代表一个有序集合,集合中每个元素都有其对应的顺序索引。List集合允许使用重复元素,可以通过索引来访问指定位置的集合元素。

List接口继承于Collection接口,它可以定义一个允许重复有序集合。因为List中的元素是有序的,所以我们可以通过使用索引(元素在List中的位置,类似于数组下标)来访问List中的元素,这类似于Java的数组。

List接口为Collection直接接口。List所代表的是有序的Collection,即它用某种特定的插入顺序来维护元素顺序。用户可以对列表中每个元素的插入位置进行精确地控制,同时可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。实现List接口的集合主要有:ArrayList、LinkedList、Vector、Stack。

(1)ArrayList

      ArrayList是一个动态数组,也是我们最常用的集合。它允许任何符合规则的元素插入甚至包括null。每一个ArrayList都有一个初始容量(10),该容量代表了数组的大小。随着容器中的元素不断增加,容器的大小也会随着增加。在每次向容器中增加元素的同时都会进行容量检查,当快溢出时,就会进行扩容操作。所以如果我们明确所插入元素的多少,最好指定一个初始容量值,避免过多的进行扩容操作而浪费时间、效率

      size、isEmpty、get、set、iterator 和 listIterator 操作都以固定时间运行。add 操作以分摊的固定时间运行,也就是说,添加 n 个元素需要 O(n) 时间(由于要考虑到扩容,所以这不只是添加元素会带来分摊固定时间开销那样简单)。

      ArrayList擅长于随机访问。同时ArrayList是非同步的。

(2)LinkedList

      同样实现List接口的LinkedList与ArrayList不同,ArrayList是一个动态数组,而LinkedList是一个双向链表。所以它除了有ArrayList的基本操作方法外还额外提供了get,remove,insert方法在LinkedList的首部或尾部。

      由于实现的方式不同,LinkedList不能随机访问,它所有的操作都是要按照双重链表的需要执行。在列表中索引的操作将从开头或结尾遍历列表(从靠近指定索引的一端)。这样做的好处就是可以通过较低的代价在List中进行插入和删除操作。

      与ArrayList一样,LinkedList也是非同步的。如果多个线程同时访问一个List,则必须自己实现访问同步。一种解决方法是在创建List时构造一个同步的List: 
List list = Collections.synchronizedList(new LinkedList(...));

(3)Vector

      与ArrayList相似,但是Vector是同步的。所以说Vector是线程安全的动态数组。它的操作与ArrayList几乎一样。

(4)Stack

     Stack继承自Vector,实现一个后进先出的堆栈。Stack提供5个额外的方法使得Vector得以被当作堆栈使用。基本的push和pop 方法,还有peek方法得到栈顶的元素,empty方法测试堆栈是否为空,search方法检测一个元素在堆栈中的位置。Stack刚创建后是空栈。

以下来自JDK1.9 关于List

Public interface List<E> extends Collection<E>

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.
一个有序集合(也称为序列)。 这个接口的用户可以精确控制每个元素插入的列表的位置。 用户可以通过他们的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。

Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.

与集合不同,列表通常允许重复的元素。更正式的说,列表通常允许成对的元素e1和e2,比如e1.equals(e2),如果它们允许零元素,它们通常允许多个空元素。有人可能希望实现一个禁止重复的列表,在用户试图插入时抛出运行时异常,这不是不可想象的,但我们希望这种用法非常少见。

The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iteratoraddremoveequals, andhashCode methods. Declarations for other inherited methods are also included here for convenience.

List接口将额外的规定放置在迭代器的契约、添加、移除、equals和hashcode方法上,超出了集合接口中指定的条款。为了方便起见,这里还包括了其他继承方法的声明。

The List 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 LinkedList 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.

List接口提供了四种方法来定位(索引)对列表元素的访问。列表(如Java数组)是基于0的。请注意,这些操作可能会与某些实现的索引值成比例(例如,LinkedList类)。因此,如果调用者不知道实现,那么遍历列表中的元素通常更可取。


The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.

List接口提供了一个特殊的迭代器,称为ListIterator,除了Iterator接口提供的常规操作之外,它还允许元素插入和替换,以及双向访问。提供了一种方法来获得列表迭代器,它从列表中指定的位置开始。

The List 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.

List接口提供了两种搜索指定对象的方法。从性能的角度来看,这些方法应该谨慎使用。在许多实现中,它们将执行代价高昂的线性搜索。



The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.

List接口提供了两种方法,可以有效地在列表中的任意一点插入和删除多个元素。

Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a list.

注意:虽然列表可以将自己作为元素来包含,但是要特别注意:在这样的列表中,equals和hashCode方法不再有很好的定义。



Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface。

一些列表实现对它们可能包含的元素有限制。 例如,有些实现禁止零元素,有些实现对其元素的类型有限制。 试图添加一个不合格的元素会抛出一个未经检查的异常,通常是NullPointerException或ClassCastException。 试图查询不合格元素的存在可能会抛出异常,或者它可能只是返回false; 一些实现将展示前者的行为,而有些实现将展示后者。 更一般的情况是,尝试对一个不符合条件的元素进行操作,其完成不会导致将一个不合格的元素插入到列表中,这可能会抛出异常,或者在实现的选项中可能成功。 在该接口的规范中,这些异常被标记为“可选”。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值