List series collection

Table of contents

        Foreword:

        1. The unique method of List collection

Second, there are several ways to traverse the List collection?

3. The underlying principle of Arraylist collection

Fourth, the characteristics of LinkedList

Foreword:

The List collection includes the JavaList interface and all implementation classes of the List interface. The elements in the List collection are allowed to be repeated, and the order of each element is the order in which the object is inserted, similar to the array in Java, and the user can access the elements in the collection by using the index (the position of the element in the collection).

The most important feature of List is order; it will ensure that elements are stored in a certain order. List adds a lot of methods on the basis of Collection, so that it can insert and delete elements in the middle of the sequence. (Only recommended for LinkedList.)

List can create a ListIterator object. In addition to using it to insert and delete elements in the middle of the List, you can also use it to traverse the List in two directions.

1. The unique method of List collection

Because the List Collection supports indexing, it has a lot of unique API s for indexing operations, and List also inherits the functions of other Collections.

 

2. Code demo:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
​
/**List
 * Arraylist  Linkedlist Ordered, repeatable, indexed
 * LinkList :API
 * addFirst(E,e)  inserts the specified element at the beginning of this list
 * addLast(E,e)  Appends the specified element to the end of this list
 * getFirst()  returns the first element of this list
 * getLast()  returns the last element of this list
 * removeFirst()  remove and return the first element
 * removeLast()  remove and return the last element
 */
​
/**Collection concurrent modification exception problem
 * When some elements are found and modified, it is easy to cause concurrent modification exceptions
 */
public class List_Demo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("JAVA");
        list.add("JAVA");
        list.add("MYsql");
        list.add("Python");
        //Add data at an index position
        list.add(2, "html");
        System.out.println(list);
        //remove element by index
        System.out.println(list.remove(2));
        System.out.println(list);
        //get element by index
        System.out.println(list.get(2));
        //Modify the element at the index position and return the data before modification
        System.out.println(list.set(1, "Myprict"));
        System.out.println(list);
    }
}

3. The underlying principle of the implementation class of List

The bottom layer of ArrayList is implemented based on arrays. It is fast to query elements, and relatively slow to add and delete;

The bottom layer of linkedList is implemented based on a double-linked list. It is slow to query elements, and it is very fast to add and delete the first and last elements.

Second, there are several ways to traverse the List collection?

①Iterator

②Enhanced for loop

③Lambda expression

      ④ for loop (because there are indexes in the List collection)

4. Code demo:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
​
public class ListDemo2 {
    public static void main(String[] args) {
        List<String> lists = new ArrayList<>();
        lists.add("JAVA");
        lists.add("JAVA");
        lists.add("MYsql");
        lists.add("Python");
        //1.for loop
        System.out.println("--------------");
        for (int i = 0; i < lists.size(); i++) {
            String ele = lists.get(i);
            System.out.println(ele);
        }
        //2. Iterators
        System.out.println("--------------");
        Iterator<String> it = lists.iterator();
        while (it.hasNext()){
            String ele = it.next();
            System.out.println(ele);
        }
        //3.foreach
        System.out.println("-----------");
        for (String ele : lists) {
            System.out.println(ele);
        }
        //4.Lambda expressions
        System.out.println("--------------");
        lists.forEach(s -> {
            System.out.println(s);
        });
    }
}

3. The underlying principle of Arraylist collection

The bottom layer of the Arraylist collection is implemented based on an array: it is fast to locate elements according to the index, and adding and deleting elements requires a shift operation of elements.

When the collection is created for the first time and the first element is added, an array with a default length of 10 is created at the bottom layer.

Fourth, the characteristics of LinkedList

The underlying data structure is a double-linked list, the query is slow, and the speed of the first and last operations is extremely fast, so there are many more unique API s for the first and last operations.

5. Unique functions of the LinkedList collection

6. Code demo:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
​
/**List
 * Arraylist  Linkedlist Ordered, repeatable, indexed
 * LinkList :API
 * addFirst(E,e)  inserts the specified element at the beginning of this list
 * addLast(E,e)  Appends the specified element to the end of this list
 * getFirst()  returns the first element of this list
 * getLast()  returns the last element of this list
 * removeFirst()  remove and return the first element
 * removeLast()  remove and return the last element
 */
​
/**Collection concurrent modification exception problem
 * When some elements are found and modified, it is easy to cause concurrent modification exceptions
 */
public class List_Demo {
    public static void main(String[] args) {
        //LinkedList can complete queue structure and stack structure (double linked list)
        //the stack
        LinkedList<String> stack = new LinkedList<>();
        //stack
        stack.push("first bullet");
        stack.addFirst("second bullet");
        stack.addFirst("third bullet");
        stack.addFirst("fourth bullet");
        stack.addFirst("fifth bullet");
        stack.addFirst("sixth bullet");
        System.out.println(stack);
        //pop out
        System.out.println(stack.removeFirst());
        System.out.println(stack.pop());
        System.out.println(stack);
        System.out.println("==================================");
        //queue
        LinkedList<String> queue = new LinkedList<>();
        //enqueue
        queue.offerLast("1 No");
        queue.addLast("2 No");
        queue.addLast("3 No");
        queue.addLast("4 No");
        queue.addLast("5 No");
        //dequeue
        System.out.println(queue.removeFirst());
        System.out.println(queue);
​
        System.out.println("=============================");
        //Combining Concurrent Modification Exceptions
        //iterator
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String ele = it.next();
            if ("JAVA".equals(ele)) {
//       list.remove("JAVA");//will report an error
                it.remove();//correct
            }
        }
        System.out.println(list);
    }
}

Tags: Java

Posted by nyi8083 on Thu, 19 Jan 2023 01:18:42 +0530