zoukankan      html  css  js  c++  java
  • java List 学习

    要学习List<E>接口,首先,我知道它还有一个父接口Collection<E>。而Collection<E>又有一个超级接口Iterable<T>。

    我们从超级接口Iterable<T>开始看:

    // 实现这个接口允许对象成为 "foreach" 语句的目标。
    public interface Iterable<T>

      方法只有一个:

      iterator()     // 返回一个在一组 T 类型的元素上进行迭代的迭代器。

    然后我们来看Collection<E>:

    public interface Collection<E> extends Iterable<E>

      解释:

         1、Collection 层次结构 中的根接口。

         2、Collection 表示一组对象,这些对象也称为 collection 的元素

        3、Collection 有些允许重复,有些不允许,有些有序,有些无序。

        4、一般操作Collection没有任何的直接实现,都是通过具体的子接口(比如Set,List)来操作它。

       所有API:

    add(E element) 确保此 collection 包含指定的元素。 
    addAll(Collection<? extends E> c) 将指定 collection 中的所有元素都添加到此 collection 中。
    clear() 从collection中移除所有元素。
    contains(Object o) 如果collection包含指定的元素,则返回 true。
    containsAll(Collection<?> c) 如果collection包含指定 collection 的所有元素,则返回 true。
    equals(Object o)  比较指定的对象与此 collection 是否相等。
    hashCode()  返回此collection的哈希码值。
    isEmpty()  如果此 collection 不包含元素,则返回 true。
    remove(Object o)  从此 collection 中移除指定元素的单个实例,如果存在的话。
    removeAll(Collection<?> c) 移除此 collection 中那些也包含在指定 collection 中的所有元素。
    retainAll(Collection<?> c) 仅保留此 collection 中那些也包含在指定 collection 的元素。
    size() 返回此 collection 中的元素数。
    toArray() 返回包含此 collection 中所有元素的数组。
    toArray(T[] a)  返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。
    iterator() 返回在此 collection 的元素上进行迭代的迭代器。(继承的方法)

    然后看List<E>:

    public interface List<E> extends Collection<E>

      解释:

        1、List<E> :有序的collection(也称为序列)。

        2、可以根据元素的整数索引访问元素。

        3、与 set 不同,列表通常允许重复的元素。

        4、List<E> 继承于collection<E>和iterator<T>,但在一些方法(iterator,add,remove,equals,hashCode)上在原方法的基础上多加了一些其他的约定。

        5、List<E> 扩展了一些父接口没有方法。

      所有API:

    继承collection的所有方法 把其中的collection改成列表,大致意思和用法不变。变的方法在下面重写。
    add(E element) 向列表的尾部添加指定的元素。 
    add(int index,E element) 在列表的指定位置插入指定元素。将当前处于该位置的元素(如果有的话)和所有后续元素向右移动(在其索引中加 1)。
    addAll(int index,Collection<? extends E> c) 将指定 collection 中的所有元素都插入到列表中的指定位置。                                  
    get(int index)  返回列表中指定位置的元素。
    indexOf(Object o)   返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。 
    lastIndexOf(Object o)  返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。
    remove(int index)  移除列表中指定位置的元素。
    remove(Object o) 从此列表中移除第一次出现的指定元素(如果存在)。
    set(int index, E element) 用指定元素替换列表中指定位置的元素。
    subList(int fromIndex, int toIndex) 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。 
    toArray()   返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)。 
    toArray(T[] a)  同collection, 且按适当顺序排列。
    iterator()  返回按适当顺序在列表的元素上进行迭代的迭代器。 
    listIterator()  返回此列表元素的列表迭代器(按适当顺序)。 
    listIterator(int index)  返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。 



       对List的一顿操作:

    复制代码
     1         Map<String, Object> map1 = new HashMap<>();
     2         map1.put("name", "xiaoming");
     3         map1.put("sex", "0");
     4         map1.put("phone", "13566668888");
     5         map1.put("email", "xiaoming@163.com");
     6         map1.put("age", "18");
     7         // List声明
     8         List<String> list1 = new ArrayList<>();
     9         List<Object> list2 = new ArrayList<>();
    10         List<Object> list3 = new ArrayList<>();
    11         // add操作
    12         list1.add("name");
    13         list1.add("email");
    14         list1.add("sex");
    15         list1.add("age");
    16         list1.add("name"); // list1: ["name","email","sex","age","name"];
    17         list2.add(map1); // list2: [{phone=13566668888, name=xiaoming,email=xiaoming@163.com, age=18}]
    18         // addAll操作
    19         list3.addAll(list1); // list3: ["name","email","sex","age","name"];
    20         list1.add(0, "phone"); // list1: ["phone","name","email","sex","age","name"];
    21         list2.addAll(0, list1); // list2: [phone, name, email, sex, age, name, {phone=13566668888, name=xiaoming, email=xiaoming@163.com, age=18}]
    22         // get操作,equals操作
    23         if (list1.get(1).equals(list3.get(0))) { // name==name
    24             // set操作,indexOf操作
    25             list3.set(list1.indexOf("phone"), "phone"); // list1.indexOf("phone")=0;list3: [phone, email, sex, age, name]
    26             // remove操作
    27             list2.remove(1); // list2: [phone, email, sex, age, name, {phone=13566668888, name=xiaoming, email=xiaoming@163.com, age=18}]
    28             list3.remove("sex"); // list3: [phone, email, age, name]
    29         }
    30         // isEmpty操作
    31         if (!list2.isEmpty()) { // list2.isEmpty()=false
    32             // removeAll操作
    33             list2.removeAll(list2); // list2: []
    34         }
    35         // contains操作
    36         if (!list2.contains("name")) { // list2.contains("name")=false
    37             // size操作
    38             int list3Size = list3.size(); // list3Size=4
    39             // lastIndexOf操作
    40             int list1LastIndexOfName = list1.lastIndexOf("name"); // list1LastIndexOfName=5
    41             // subList操作
    42             list2.addAll(list1.subList(list3Size, list1LastIndexOfName)); // list2: [age]
    43         }
    44         // containsAll操作
    45         if (list1.containsAll(list3)) { // list1.containsAll(list3)=true
    46             // toArray()
    47             map1.put("arr", list1.toArray()); // list1.toArray(): [phone, name, email, sex, age, name]
    48             // toArray(T[] a)
    49             // toArray(new Object[0])在功能上和toArray()相同; list1.toArray(new Object[0]): [phone, name, email, sex, age, name]
    50             map1.put("arr2", list1.toArray(new Object[0])); // 搞不懂这个怎么用,想了解的你们自己研究~会了然后回来教我
    51             // retainAll操作
    52             list1.retainAll(list3); // list1: [phone, name, email, age, name]
    53             // clear操作
    54             list3.clear(); // list1: []
    55             // hashCode操作
    56             map1.put("hashCode", list1.hashCode()); // list1.hashCode(): -912778582
    57         }
    复制代码

    至于iterator(),我不会,有兴趣的同学们自己研究,研究会了然后回来教我~

  • 相关阅读:
    MySQL_01 常用命令
    32_Go基础(TCP通信)
    oracle查询优化
    Eclipse中自动添加注释(作者,时间)
    java注解的学习
    JqueryeasyUIdatagrid参数之 queryParams
    Eclipse中,打开文件所在文件夹的插件,及设置
    更改Zend Studio/Eclipse代码风格主题
    JAVA中使用File类批量重命名文件及java.io.File的常见用法
    java面试笔试题大全
  • 原文地址:https://www.cnblogs.com/dudadi/p/7912050.html
Copyright © 2011-2022 走看看