Detailed set of Java-List

List collection presentation

List Collections Overview

  List ordered set is an element (index of each element has a sequence corresponding to the first element index is 0), and repeatable set.

List the set of common methods

  List is a sub-interface Collection interface Collection has all of the methods, there are some methods of index operations.

  • void add(int index, E element);: The element of the element inserted into the List index set;
  • boolean addAll(int index, Collection<? extends E> c);: The set of all elements c are inserted into the beginning of the List index set;
  • E remove(int index);: Removes and returns the element at index;
  • int indexOf(Object o);: Returns the index position in the List object o the first occurrence of the collection;
  • int lastIndexOf(Object o);: Returns the index of the last position of the object o in the List collection occurring;
  • E set(int index, E element);: The element at index index and replaced with a new element of the object, and 返回被替换的旧元素;
  • E get(int index);: Returns the index of the index set;
  • List<E> subList(int fromIndex, int toIndex);: Returns the index subset from fromIndex (inclusive) to index toIndex (exclusive contain) consisting of all elements;
  • void sort(Comparator<? super E> c) : List sort collection element according Comparator parameter;
  • void replaceAll(UnaryOperator<E> operator) : Resets all elements of the set according to the calculation rules specified operator.
  • ListIterator<E> listIterator();: Returns a ListIterator object that inherits the interface Iterator interface, a method of increasing the Iterator interface based on the iterative forward functions and elements may be increased:
    bookean hasPrevious(): whether to return an iterator associated to an element set there;
    E previous();: returns an iterator an upper element; a
    void add(E e);: the element is inserted in the specified location;

Examples

1) Run Main

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) Run results:

原列表:[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]

  Seen from the above operating results, System.out.println("判断newBook002的位置:" + list.indexOf(new String("newBook002")));we re a new "newBook002" for the index position is determined, or can return to the index position, List considerable collection of objects is determined only by the two equals()methods, if the object override equals()methods are true, then deposit List the objects in the collection are in fact equal.

Guess you like

Origin www.cnblogs.com/Andya/p/12638480.html