Collection 2-List

Knocked on by hand, there may be some deviations, forgive me

Collection frame

Features of List collection

-Is a single-column collection, and is a sub-interface of the Collection interface. All methods in the Collection are available here.
-Several more methods than Collection.
-In the List interface, the concept of subscripts is highlighted.

Retouch & Return method description
void add(int index, E element) Insert an element into the specified subscript position in the collection.
void addAll(int index, Collection<E> coll) Insert the subscript specified in the collection into all the data in another collection.
E remove(int index) Delete the element with the specified subscript position in the collection.
E set(int index, E element) Modify the value of the specified subscript.
E get(int index) Get the element with the specified subscript.
int indexOf(E element) Get the subscript of the first occurrence of an element in the collection.
int lastIndexOf(E element) Get the last subscript of an element in the collection.
List<E> subList(int fromIndex, int toIndex) From a collection, part of it is taken as a sub-collection. [from, to).
void replaceAll(UnaryOperator<E> operator) Bring the elements of the collection into the interface method and replace the original elements with the return value.
void sort(Comparator<E> comparator) Sort the elements in the collection in ascending order.

Sample code

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String>list=new ArrayList<>();
        list.add("lily");
        list.add("poly");
        list.add("lucy");
        System.out.print("list集合中的元素:");
        for (String i : list) {
            System.out.print(i+" ");
        }
        System.out.println();
        System.out.println("===================");

        list.add(2,"tom");
        System.out.println("在2号位置插入tom后的list集合");
        for (String i : list) {
            System.out.print(i+" ");
        }
        System.out.println();
        System.out.println("===================");

        list.addAll(2,list);
        System.out.println("在2号位置插入lsit集合后的list集合");
        for (String i : list) {
            System.out.print(i+" ");
        }
        System.out.println();
        System.out.println("===================");
        //若删除成功返回删除后的元素
        System.out.println("被删除的元素是:"+list.remove(2));

        //修改指定下标位的值
        // 返回被覆盖的值。
        System.out.println("被覆盖的元素是:"+list.set(2, "AAA"));

        //获取指定下标位的元素。
        System.out.println("2号位的元素是:"+list.get(2));

        //获取集合中的某一个元素第一次出现的下标
        System.out.println("lucy第一次出现在"+list.indexOf("lucy"));

        //获取集合中的某一个元素最后一次出现的下标
        System.out.println("lucy最后一次出现在"+list.lastIndexOf("Jim"));

        //从一个集合中,截取一部分,作为子集合。 [from, to)
        List<String> sub = list.subList(2, 6);
        System.out.print("截取的元素为:");
        for (String i : sub) {
            System.out.print(i+" ");
        }
    }
}

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String>list=new ArrayList<>();
        list.add("lily");
        list.add("poly");
        list.add("lucy");
        System.out.print("list集合中的元素:");
        for (String i : list) {
            System.out.print(i+" ");
        }
        System.out.println();
        System.out.println("===================");
        //元素替换
        //     将集合中的每一个元素,带入到接口的方法中,用返回值替换原来的元素
        list.replaceAll(ele -> ele.concat(".txt"));
        //证明List字底层重写了toString方法,所以可以直接使用输出语句输出数据
        //若没有重写,则应该输出的是一个地址
        System.out.println(list);
    }
}

Sorting of collections

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        // 2. 增元素
        list.add("Lily");
        list.add("Lucy");
        list.add("Polly");
        list.add("Jim");

        // 排序
        list.sort((e1, e2) -> e1.length() - e2.length());
    }
}

Guess you like

Origin www.cnblogs.com/dch-21/p/12723386.html