java.util.List<E> 源码分析(JDK1.7)

---------------------------------------------------------------------------------

java.util.List<E>是一个接口,它的定义如下:

 public interface List<E> extends Collection<E> {
// Query Operations // Modification Operations // Bulk Modification Operations // Comparison and hashing // Positional Access Operations // Search Operations // List Iterators // View
}

(1)List列表是一个有序的collection,此接口可以对列表中每个元素的插入位置进行精确地控制

(2)用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索List列表中的元素

(3)List列表允许重复的元素

(4)List接口提供了特殊的迭代器,称为ListIterator,除了允许Iterator接口提供的正常操作外,该迭代器还允许元素插入和替换,以及双向访问

(5)看看java.util.Collection<E>的源码介绍

---------------------------------------------------------------------------------

下面来看看java.util.List<E>中具体有哪些方法

从下面的表格中可以看出java.util.List<E>接口中一共有25个方法:

  其中查询操作6个;修改操作2个;批量操作6个;比较和哈希操作2个;位置访问操作4个;位置查询操作2个;List Iterator操作2个;视图操作1个;(用浅蓝色字体标出的是java.util.List<E>接口新增的方法,其余的都是从java.util.Collection<E>中来的。  (~_~ 真是不嫌事情多,提供了25个方法需要由其扩展类来实现...))

      查询操作 int size() 返回列表中的元素数。如果列表包含多于Integer.MAX_VALUE个元素,则返回Integer.MAX_VALUE 
boolean isEmpty()  如果列表不包含元素,则返回true
boolean contains(Object o)  如果列表包含指定的元素,则返回true。
Iterator<E> iterator()  返回按适当顺序在列表的元素上进行迭代的迭代器
Object[] toArray()  返回按适当顺序包含列表中的所有元素的数组
<T> T[] toArray(T[] a)  返回按适当顺序包含列表中所有元素的数组
     修改操作 boolean add(E e)  向列表的尾部添加指定的元素
boolean remove(Object o)  从此列表中移除第一次出现的指定元素
      批量操作 boolean containsAll(Collection<?> c)  如果列表包含指定collection的所有元素,则返回true
boolean addAll(Collection<? extends E> c)  添加指定collection中的所有元素到此列表的结尾
boolean addAll(int index,Collection<? extends E> c)  将指定collection中的所有元素都插入到列表中的指定位置
boolean removeAll(Collection<?> c)  从列表中移除指定collection中包含的其所有元素
boolean retainAll(Collection<?> c)  仅在列表中保留指定collection中所包含的元素
void clear()  从列表中移除所有元素
  比较和哈希操作 boolean equals(Object o)  比较指定的对象与列表是否相等
int hashCode()  返回列表的哈希码值
 位置访问操作    E get(int index)  返回列表中指定位置的元素
E set(int index,E element)  用指定元素替换列表中指定位置的元素
void add(int index,E element)  在列表的指定位置插入指定元素
E remove(int index)  移除列表中指定位置的元素
  位置查询操作 int indexOf(Object o)  返回此列表中第一次出现的指定元素的索引
int lastIndexOf(Object o)  返回此列表中最后出现的指定元素的索引
  List Iterators ListIterator<E> listIterator()  返回此列表元素的列表迭代器
ListIterator<E> listIterator(int index)  返回列表中元素的列表迭代器
 视图操作 List<E> subList(int fromIndex,int toIndex)  返回列表中指定的fromIndex(包括)和toIndex(不包括)之间的部分视图

再来看看下图:

---------------------------------------------------------------------------------

java.util.List<E>源码如下:(看看下面的源码,定义的很规范,各种操作都有-----> 此时应该想到它的实现类该有多可怜,要实现多少方法呀。~_~)

 package java.util;

 public interface List<E> extends Collection<E> {
// Query Operations
int size(); boolean isEmpty(); boolean contains(Object o); Iterator<E> iterator(); Object[] toArray(); <T> T[] toArray(T[] a); // Modification Operations
boolean add(E e); boolean remove(Object o); // Bulk Modification Operations
boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); boolean addAll(int index, Collection<? extends E> c); boolean removeAll(Collection<?> c); boolean retainAll(Collection<?> c); void clear(); // Comparison and hashing
boolean equals(Object o); int hashCode(); // Positional Access Operations
E get(int index); E set(int index, E element); void add(int index, E element); E remove(int index); // Search Operations int indexOf(Object o); int lastIndexOf(Object o); // List Iterators
ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); // View
List<E> subList(int fromIndex, int toIndex);
}

---------------------------------------------------------------------------------

---------------------------------------------------------------------------------

---------------------------------------------------------------------------------

---------------------------------------------------------------------------------

最新文章

  1. 面向服务的体系结构(SOA)——(1)目标与核心概念
  2. 黄聪:wordpress自定义post_type,并且自定义固定链接
  3. H5下拉刷新特效demo,动画流畅
  4. android 关于InputDispatcher出现Consumer错误的解决办法
  5. poj 3268 Silver Cow Party(最短路)
  6. BZOJ 1563 诗人小G
  7. (转载)PHP去掉转义后字符串中的反斜杠\函数stripslashes
  8. WebService 通用接收方法
  9. tomcat使用说明
  10. C++中struct和class的总结
  11. Oracle Split 函数
  12. react.js 从零开始(四)React 属性和状态详解
  13. hrbustoj 2013 Play Game 2(博弈)
  14. JAVAEE——SSH项目实战02:客户列表和BaseDao封装
  15. SQL操作符、通配符等
  16. mongo 联表查询
  17. H/s:哈希率单位转换
  18. pytorch基础
  19. day6 字典
  20. Spark2.2(三十八):Spark Structured Streaming2.4之前版本使用agg和dropduplication消耗内存比较多的问题(Memory issue with spark structured streaming)调研

热门文章

  1. 51nod算法马拉松 contest7
  2. 3、手把手教你Extjs5(三)MVVM特性的简单说明
  3. mvc中上传图片到指定文件夹中
  4. C#生成随机验证吗例子
  5. CSS中怎么让DIV居中
  6. 5)Javascript设计模式:extends模式
  7. UVa 459 - Graph Connectivity
  8. Eclipse配置Git发布项目到Github
  9. [HNOI2004]Language L语言
  10. iOS 之 时间格式与字符串转换