zoukankan      html  css  js  c++  java
  • JDK源码(1.7) -- java.util.List<E>

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

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

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

     1 public interface List<E> extends Collection<E> {
     2     // Query Operations
     3 
     4     // Modification Operations
     5 
     6     // Bulk Modification Operations
     7 
     8     // Comparison and hashing
     9 
    10     // Positional Access Operations
    11 
    12     // Search Operations
    13 
    14     // List Iterators
    15 
    16     // View
    17 }

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

     1 package java.util;
     2 
     3 public interface List<E> extends Collection<E> {
     4     // Query Operations
     5     int size();
     6 
     7     boolean isEmpty();
     8 
     9     boolean contains(Object o);
    10 
    11     Iterator<E> iterator();
    12 
    13     Object[] toArray();
    14 
    15     <T> T[] toArray(T[] a);
    16 
    17 
    18     // Modification Operations
    19     boolean add(E e);
    20 
    21     boolean remove(Object o);
    22 
    23 
    24     // Bulk Modification Operations
    25     boolean containsAll(Collection<?> c);
    26 
    27     boolean addAll(Collection<? extends E> c);
    28 
    29     boolean addAll(int index, Collection<? extends E> c);
    30 
    31     boolean removeAll(Collection<?> c);
    32 
    33     boolean retainAll(Collection<?> c);
    34 
    35     void clear();
    36 
    37 
    38     // Comparison and hashing
    39     boolean equals(Object o);
    40 
    41     int hashCode();
    42 
    43 
    44     // Positional Access Operations
    45     E get(int index);
    46 
    47     E set(int index, E element);
    48 
    49     void add(int index, E element);
    50 
    51     E remove(int index);
    52 
    53 
    54     // Search Operations
    55 
    56     int indexOf(Object o);
    57 
    58     int lastIndexOf(Object o);
    59 
    60 
    61     // List Iterators
    62     ListIterator<E> listIterator();
    63 
    64     ListIterator<E> listIterator(int index);
    65 
    66     // View
    67     List<E> subList(int fromIndex, int toIndex);
    68 }

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

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

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

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

  • 相关阅读:
    并查集N(The Suspects)
    (并查集)Ubiquitous Religions
    (并查集)How Many Tables
    并查集(畅通工程)
    约瑟夫环(栈和队列)
    队列-排队买饭
    栈的基本操作
    双向队列
    括号匹配
    Queue
  • 原文地址:https://www.cnblogs.com/xinhuaxuan/p/6341645.html
Copyright © 2011-2022 走看看