Collection接口的子接口——List接口

  https://docs.oracle.com/javase/8/docs/api/java/util/List.html

 

  public interface List<E> extends Collection<E>

  E是List管理的元素类型。

  父接口:Collection<E>, Iterable<E>

  实现类:ArrayList、LinkedList、Stack、Vector 、RoleList

 

  一、简介

  List是一个有序集合,也称为顺序表。

  接口的用户可以精确控制元素插入List到中的位置,也可以通过位置下标直接访问元素。

 

  List通常允许包含重复的元素,如果允许null元素,那么也可以包含重复的null元素。

  List提供了4种方法来通过位置索引访问元素。与数组一样,位置索引从0开始。

  如果调用者不知道具体List的实现,遍历(迭代)List中的元素通常是更好的选择。(时间开销对不同的实现有不同。)

 

  List接口提供了一个特殊的迭代器,叫ListIterator。除了有Iterator接口中的迭代器的常规功能外,它还允许元素插入、替换和进行双向访问。List还能提供一个从指定位置开始的ListIterator。

 

  List接口提供了两种方法来查询特定的元素,从性能的角度来看,应该谨慎使用它们,因为在很多实现类中这将执行代价昂贵的线性搜索。

  List提供了两种方法来从任意位置进行多个元素的插入和删除。

 

  需要注意的是,如果一个List实现类允许将自身作为集合元素,equals()方法和hashcode()方法就不能被很好地定义了。

 

  该接口是Java Collections Framwork的一员。(所以之前是理解错了,并不是所有相关接口都是其中的一员…)

 

  二、方法

  基本方法与Set中的方法差不多,考虑到List是有序集合,所以通过方法得到的迭代器、数组中的元素都是有序的。

  介绍特殊的方法:

  1、default void replaceAll(UnaryOperator<E> operator)

  UnaryOperator<E>是一个功能性(Function)接口,表示一个输入输出的是同一类型的一元运算符(如++)。

  这个方法表示什么呢?即把operator代表的一元操作,应用到List集合中的每一个元素上,然后取代原来的元素。

final ListIterator<E> li = list.listIterator();
     while (li.hasNext()) {
         li.set(operator.apply(li.next()));
     }
 /*如果 list's list-iterator不支持set操作,会引发异常*/
/*If the list's list-iterator does not support the set operation then an UnsupportedOperationException will be thrown when replacing the first element.*/

 

  2、default void sort(Comparator<? super E> c)

  根据指定的Comparator(比较器)对List集合中的元素进行排序。

  所以前提是集合中的元素,相互之间可以用这个Comparator进行比较。(that is, c.compare(e1, e2) must not throw a ClassCastException(类型强制转换异常) for any elements e1 and e2 in the list)

 

  如果指定的比较器为null,则此列表中的所有元素必须实现Comparable接口,并且应该使用元素的自然顺序(natural ordering)。

  This list must be modifiable, but need not be resizable.

  注意:

  This implementation is a stable, adaptive, iterative mergesort that requires far fewer than nlg(n) comparisons 

 

  3、boolean equals(Object o)

  如果两个List集合包含的元素相同,且元素的顺序相同,则称这两个list相等~

 

  4、int hashcode()

  一个List集合的hashcode是按如下定义计算的:

int hashCode = 1;
for (E e : list)
     hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());

 

  这样可以确保equals()相等的两个list的hashcode也相等,如object类中hashcode规约所要求的那样。

 

  5、E get(int index)

  Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

  根据这个异常的提示,我确认了集合没有“容量大小”的概念,size()返回的是存在的元素个数,且index基于0。

 

  6、E set(int index, E element)

  可选操作(总算搞懂了可选操作的含义,即实现类未必支持这个操作,如果不支持时就会报错UnsupportedOperationException)。

  

  7、void add(int index, E element)

  可选操作。

  在指定位置插入指定元素,然后就该位置原来的以及后面的元素右移一位。

 

  8、E remove(int index)

  可选操作。

  移除并返回指定位置的元素,然后将后面的元素前移。

 

  9、int indexof(Object o)

  返回此List中指定元素第一次出现的索引,如果此列表不包含元素,则返回-1。

 

  10、int lastIndexOf(Object o)

  返回此List中指定元素最后一次出现的索引,如果此列表不包含元素,则返回-1。

 

  11、ListIterator<E> listIterator()

  返回此List中元素的list 迭代器(按正确的顺序)

 

  12、ListIterator<E>  listIterator(int index)

  返回的list迭代器从指定位置开始。即第一次调用next()方法返回index索引处的元素,而第一次调用previous()方法返回index-1索引处的元素。

  (ListIterator<E>接口,了解一下)

 

  13、List<E> subList(int fromIndex, int toIndex)

  返回该List的一个视图,即它的子list。

  这个子list是由源list支持的,所以对子list的非结构性修改,也会应用到源list;反之亦然。

  子list同支持在源list中支持的“可选操作”。

  

  如果源list,通过其它方式而不是通过子list,进行了结构性修改,那么此方法返回的子list的语义也变得模糊(???)

 

  什么是“结构性修改”呢?

  Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results

  

 

  14、default Spliterator<E> spliterator()

  因为没理解这个方法所有我又写了。

 

 

 

  

 

 

 

 

 

 

 

 

 

  

原文地址:https://www.cnblogs.com/bigbigbigo/p/8564982.html