The List collection

The java.util.List extends Collection interface

List interface features:

  • An ordered collection in which elements are stored and retrieved in the same order
  • There are indexes, including some indexed methods
  • Allows storing duplicate elements

Indexed methods in the List interface (unique)

  • public void add(int index, E element); Adds the specified element to the specified position in the collection.
  • public E get(int index); Returns the element in the collection at the specified position.
  • public E remove(int index); Removes the element at the specified position in the list. Returns the removed element.
  • public E set(int index, E element); Replaces the element at the specified position in the collection with the specified element, returning the element before the update.

Note:

When working with indexes, it is important to prevent index out-of-bounds exceptions.

Public static void main(String[] args) {// Create a List of objects, polymorphic List<String> List = new ArrayList<>(); // Add the list. Add ("a") element to the collection using the add method; list.add("b"); list.add("c"); list.add("d"); list.add("a"); System.out.println(list); // public void add(int index, E element); Adds the specified element to the specified position in the collection. // Add DNA list.add(3, "DNA") between c and d; System.out.println(list); //public E remove(int index); Removes the element at the specified position in the list. Returns the removed element. String removeE = list.remove(2); System.out.println(" Removed element: "+ removeE); System.out.println(list); //public E set(int index, E element); Replaces the element at the specified position in the collection with the specified element, returning the element before the update. SetE = list.set(4, "a "); setE = list.set(4," a "); System.out.println(" replaced element: "+ setE); System.out.println(list); For (int I = 0; for (int I = 0; i < list.size(); i++) { //public E get(int index); Returns the element in the collection at the specified position. String s = list.get(i); System.out.println(s); } System.out.println("--------------------"); Iterator<String> it = list.iterator(); while (it.hasNext()) { String str = it.next(); System.out.println(str); } System.out.println("--------------------"); // Use the enhancement for for (String s: list) {system.out.println (s); }} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [A, B, C, D, a] [A, b, C, DNA, D, a] a [a, b, DNA, d, A] a b DNA d A -------------------- a b DNA d A -------------------- a b DNA d ACopy the code

LinkedList

Java.util.LinkedList implements List

1. The bottom layer is a linked list structure: slow query, fast add and delete. 2. It contains a number of methods for manipulating the first and last elements. Note: Polymorphism cannot be used using methods specific to the LinkedList collection

Common methods:

  • Public void addFirst(E E) inserts the specified element at the beginning of the list.

  • Public void addLast(E E) adds the specified element to the end of the list.

  • Public void push(E E) pushes the element onto the stack represented by this list.

  • Public E getFirst() returns the first element of this list.

  • Public E getLast() returns the last element of this list.

  • Public E removeFirst() removes and returns the first element of this list.

  • Public E removeLast() removes and returns the last element of this list.

  • Public E pop() pops an element from the stack represented by the list.

  • Public Boolean isEmpty() Returns true if the list contains no elements.

public static void main(String[] args) { show01(); } /* * -public void addFirst(E E) inserts the specified element at the beginning of this list. * -public void addLast(E E) adds the specified element to the end of this list. * -public void push(E E) pushes the element onto the stack represented by this list. Linkedlist <String> linked = new linkedList <>(); linkedList <>(); // Use the add method to add elements linked. Add ("a"); linked.add("b"); linked.add("c"); System.out.println(linked); // public void addFirst(E E) inserts the specified element at the beginning of the list. linked.addFirst("www"); linked.push("com"); // Equivalent to addFirst(E E) system.out.println (linked); // public void addLast(E E) adds the specified element to the end of the list. linked.addLast("cn"); // Equivalent to add system.out.println (linked); System.out.println("----------------------"); show02(); } /* * -public E getFirst() returns the first element of this list. * -public E getLast() returns the last element of this list. */ private static void show02() {linkedList <String> linked = new linkedList <>(); // Use the add method to add elements linked. Add ("a"); linked.add("b"); linked.add("c"); System.out.println(linked); linked.clear(); //public Boolean isEmpty() Returns true if the list contains no elements. if (! linked.isEmpty()) { String first = linked.getFirst(); System.out.println(first); String last = linked.getLast(); System.out.println(last); } System.out.println("---------"); show03(); } /* * -public E removeFirst() removes and returns the first element of this list. - public E removeLast() removes and returns the last element of this list. - public E pop() Pops an element from the stack represented by this list. Linkedlist <String> linked = new linkedList <>(); // Use the add method to add elements linked. Add ("a"); linked.add("b"); linked.add("c"); //String first = linked.removeFirst(); String first = linked.pop(); System.out.println(" the first element removed is "+ first); System.out.println(linked); String last = linked.removeLast(); System.out.println(" Last element removed is "+ last); System.out.println(linked); } ----------------------------------------------------------------------------------------------------------------- [a, B, c] [com, WWW, a, b, c] [com, WWW, a, b, c, cn] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (a, b, c) -- -- -- -- -- -- -- -- -- has been removed is the first element of a [b, C] The last element to be removed is c [b]Copy the code

HashSet collection

The java.util.Set extends Collection interface

Duplicate elements are not allowed to be stored. There are no indexes, no indexed methods, and no regular for loops. Java.util. HashSet collection implements Set interface

HashSet features:

  • Duplicate elements are not allowed to be stored.
  • There are no indexes, no indexed methods, and no regular for loops.
  • Is an unordered collection, storing and retrieving elements may differ.
  • At the bottom is a hash table structure (very fast queries).
public static void main(String[] args) { Set<Integer> set = new HashSet<>(); // Use the add method to add set.add(1); set.add(3); set.add(2); set.add(1); Iterator<Integer> it = set.iterator(); while (it.hasNext()) { Integer nnn = it.next(); System.out.println(nnn); } / / used to enhance the for traverse System. The set set out. The println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "); for (Integer i : set) { System.out.println(i); }} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1 2 3 --------------- 1 2 3Copy the code

The last

Welcome to pay attention to the public number: the future has light, receive a line of large factory Java interview questions summary + the knowledge points learning thinking guide + a 300 page PDF document Java core knowledge points summary! These are some of the things that the interviewer should ask during the interview. These include basics, Java collections, JVMS, multi-threaded concurrency, Spring principles, microservices, Netty and RPC, Kafka, diaries, design patterns, Java algorithms, databases, Zookeeper, distributed caching, data structures, and more.