Java—List集合详解

List集合介绍

List集合概述

  List集合是一个元素有序(每个元素都有对应的顺序索引,第一个元素索引为0)、且可重复的集合。

List集合常用方法

  List是Collection接口的子接口,拥有Collection所有方法外,还有一些对索引操作的方法。

  • void add(int index, E element);:将元素element插入到List集合的index处;
  • boolean addAll(int index, Collection<? extends E> c);:将集合c所有的元素都插入到List集合的index起始处;
  • E remove(int index);:移除并返回index处的元素;
  • int indexOf(Object o);:返回对象o在List集合中第一次出现的位置索引;
  • int lastIndexOf(Object o);:返回对象o在List集合中最后一次出现的位置索引;
  • E set(int index, E element);:将index索引处的元素替换为新的element对象,并返回被替换的旧元素
  • E get(int index);:返回集合index索引处的对象;
  • List<E> subList(int fromIndex, int toIndex);:返回从索引fromIndex(包含)到索引toIndex(不包含)所有元素组成的子集合;
  • void sort(Comparator<? super E> c) :根据Comparator参数对List集合元素进行排序;
  • void replaceAll(UnaryOperator<E> operator) :根据operator指定的计算规则重新设置集合的所有元素。
  • ListIterator<E> listIterator();:返回一个ListIterator对象,该接口继承了Iterator接口,在Iterator接口基础上增加了以下方法,具有向前迭代功能且可以增加元素:
    bookean hasPrevious():返回迭代器关联的集合是否还有上一个元素;
    E previous();:返回迭代器上一个元素;
    void add(E e);:在指定位置插入元素;

示例

1)运行主类

public class DemoApplication {

    public static void main(String[] args) {

        List<String> list = new ArrayList();
        list.add(new String("book001"));
        list.add(new String("book002"));
        list.add(new String(" book003 "));
        System.out.println("原列表:" + list);

        //将新字符串插入第二个位置
        list.add(1, new String("newBook002"));
        System.out.println("新增第二个位置元素后列表:" + list);

        //删除第三个元素
        list.remove(2);
        System.out.println("删除第三个元素后列表:" + list);

        //判断指定元素在List集合的位置
        System.out.println("判断newBook002的位置:" + list.indexOf(new String("newBook002")));

        //将第二元素替换新的字符串
        System.out.println("替换的旧值:" + list.set(1, new String("book002")));
        System.out.println("替换第二个元素后的列表:" + list);

        //返回第二个元素
        System.out.println("回第二个元素:" + list.get(1));

        List<String> newList = new ArrayList<>();
        newList.add("book001");
        newList.add("book004");
        newList.add("book002");

        //新增集合
        list.addAll(1, newList);
        System.out.println("新增一个集合后的列表:" + list);

        //返回元素最后一次出现的位置索引
        System.out.println("返回\"book001\"最后一次出现的位置:" + list.lastIndexOf("book001"));

        //截取子集合
        System.out.println("返回一个范围子集合列表:" + list.subList(0, 3));

        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //逆序
                return o2.compareTo(o1);
            }
        });

        //lambda表达式输出
        list.forEach(book -> System.out.println(book));

        list.replaceAll(String::trim);
        System.out.println("replaceAll去除两端空格" + list);

        list.replaceAll(t -> t.replace("book00", "书籍系列"));
        System.out.println("replaceAll替换字符串:" + list);

        System.out.println("正向迭代输出:");
        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(listIterator.next());
            //添加元素,会影响list元素
            listIterator.add("book");
        }
        System.out.println("反向迭代输出:");
        while(listIterator.hasPrevious()) {
            System.out.println(listIterator.previous());
        }

        System.out.println(list);
    }

}

2)运行结果:

原列表:[book001, book002,  book003 ]
新增第二个位置元素后列表:[book001, newBook002, book002,  book003 ]
删除第三个元素后列表:[book001, newBook002,  book003 ]
判断newBook002的位置:1
替换的旧值:newBook002
替换第二个元素后的列表:[book001, book002,  book003 ]
回第二个元素:book002
新增一个集合后的列表:[book001, book001, book004, book002, book002,  book003 ]
返回"book001"最后一次出现的位置:1
返回一个范围子集合列表:[book001, book001, book004]
book004
book002
book002
book001
book001
 book003 
replaceAll去除两端空格[book004, book002, book002, book001, book001, book003]
replaceAll替换字符串:[书籍系列4, 书籍系列2, 书籍系列2, 书籍系列1, 书籍系列1, 书籍系列3]
正向迭代输出:
书籍系列4
书籍系列2
书籍系列2
书籍系列1
书籍系列1
书籍系列3
反向迭代输出:
book
书籍系列3
book
书籍系列1
book
书籍系列1
book
书籍系列2
book
书籍系列2
book
书籍系列4
[书籍系列4, book, 书籍系列2, book, 书籍系列2, book, 书籍系列1, book, 书籍系列1, book, 书籍系列3, book]

  从上述运行结果看出,System.out.println("判断newBook002的位置:" + list.indexOf(new String("newBook002")));我们重新new一个"newBook002"进行判断索引位置时,还是可以返回索引位置,List集合判断两个对象相当只通过equals()方法,所以如果重写对象的equals()方法都是true,则存入List集合中的对象其实都是相等的。

猜你喜欢

转载自www.cnblogs.com/Andya/p/12638480.html