List series collection and Set series collection

1. List series collection

1. Features

2. Unique methods

2. Traversal of List collection

Because the List collection has an index, it can be traversed using a for loop

These functions can be used by List implementation classes (LinkList, ArrayList).

3. The underlying principle of the ArrayList collection

1.Principle

2. Applicable scenarios

4. The underlying principle of the LinkList collection

1. Linked list

2. Characteristics of linked lists

2.1 Add data

2.2 Note:

Feature 1: Even if you use index query, you have to start over again

2.3 Delete data

3. Single linked list and double linked list

Features of doubly linked list:

4. About LinkLIst

5. Applicable scenarios of LinkList

1. Used to design queues

2. Used to design stacks

Source code of push() and pop()

//push method source code
public void push(E e){
    addFist(e);
}

//pop method source code
public E pop() {
    return removeFirst();
}

5. Test series collection

1. Characteristics and unique methods

2. Example

No Set set = new Set<>(); Because Set is an interface, you cannot directly new the object, you must use its implementation class

6. The underlying principle of HashSet collection (implemented based on hash table)

1.Hash value

1.1 Understanding hash values

Hash value: A value of type int. In Java, each object has a hash value.

All objects can call the hashCode method provided by the Object class to return their own hash value.

1.2 Characteristics of hash values

2.The underlying principle of HashCode

2.1 Hash table

HashCode is implemented based on hash table

Hash table is a data structure with good performance for adding, deleting, modifying and querying data.

2.2 Principle

Before JDK8

The horizontal ones are arrays, and the vertical ones are linked lists.

Therefore, it can be explained why the elements added by HashCode are disordered, unindexed, non-duplicated, and fast to add, delete, modify, and query data. (Dark Horse Basics Episode 140)

question:

Expansion 16*0.75

JDK8 and later

Tree is also a data structure, so I won’t explain too much here (Episode 140)

2.3 In-depth understanding of the deduplication mechanism of HashSet collections

as follows:

7. The underlying principle of the LinkHashSet collection

Disadvantages: But each element takes up more memory

8. The underlying principle of TreeSet collection

9. Summary

10. Concurrent modification exception of collection

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

public class CollectionTest1 {
public static void main(String[] args) {
// Master the concurrent modification exception of the collection
List<String> list = new ArrayList<>();
list.add("Xiao Wang");
list.add("Xiao Li");
list.add("Brother Li");
list.add("Xiao Liu");
list.add("Li Ming");
list.add("Li Hongzhang");
//Request to remove names with the character Li and delete them
//1. Delete using for loop
// for (int i = 0; i < list.size(); i + + ) {
// String name = list.get(i);
// if (name.contains("李")) {
// list.remove(name);
//i--;//Be sure to add i--, otherwise an exception will occur
//}
//}
//2. Use iterator
Iterator<String> it = list.iterator();
while(it.hasNext()) {
String name =it.next();
if (name.contains("李")) {
// list.remove(name); will issue a concurrent modification exception error
//So use the following line of code
it.remove();//Delete the data currently traversed by the iterator. After each data is deleted, it is equivalent to doing i-- on the bottom layer.
}
}
System.out.println(list);
}
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeSetSet Interface 139364 people are learning the system