Some collection-related knowledge encountered in java

Collection (interface):

                  List: an ordered collection that can store repeated data (in what order to store it, in what order to take it out)

                          ArrayList: underlying array, thread-safe, high search efficiency, fast access, suitable for query, ordered and repeatable

                          LinkedList: The underlying doubly linked list, thread-safe, efficient in adding and deleting, random access, suitable for adding and deleting, orderly and repeatable

                          Vector: underlying array, thread-safe, inefficient, ordered and repeatable

                  Set: unordered set, duplicate data is not allowed to be stored (the order of deposit and removal is different)

                          HashSet: underlying array + linked list + red-black tree, unordered and non-repeatable

                          TreeSet: The underlying red-black tree, and implements the SortedSet interface, can sort elements (the stored elements can be automatically sorted according to the size of the elements), and the order cannot be repeated.

List adds many methods to Collection and can be traversed using for. Set is exactly the same as Collection. Set uses iterator traversal, which has low retrieval efficiency, high deletion and insertion efficiency, and the position remains unchanged.

Map (interface): an unordered collection that stores data in the form of key-value pairs. Key objects are not allowed to be repeated, but value objects can be repeated.

         HashMap: underlying array + linked list + red-black tree, achieving asynchronous, thread-unsafe, disordered storage, key is not repeated, key-value is stored in Entry, HashMap is implemented based on hash table, and can insert null key or value , check whether the same key already exists when inserting

If it does not exist, insert it directly; if it is inserted, the new value replaces the old value.

         TreeMap: underlying red-black tree, thread-unsafe, sorted by key, keys are not repeated

HashTable: thread-safe, unordered storage, no duplicate keys

IdentityHashMap: key is repeatable, both key and value are allowed to be null, and unordered threads are not safe

WeakHashMap: Map collection of weak references

List (interface) inherits Collection (interface) orderly and repeatable

Set (interface) inherits Collection (interface), unordered and non-repeatable

Collection is the parent interface of List and Set and is generally not used directly.

Thread-safe Map: HashTable, SynchronizedMap, ConcurrentHashMap

Stack and enumeration are also thread-safe

The index of ResultSet array starts from 1

Vector is implemented through arrays, supports thread synchronization, and is internally locked, but has low efficiency and is thread-safe. When expanding the space, it will be doubled.

ArrayList is implemented through arrays. It is not thread-safe and can store duplicate data without synchronization. When expanding the space, it will expand by 50% of the original size, which is beneficial to saving space.

ThreadLocalMap uses open address method to handle hash collisions

HashMap uses the detached linked list method to handle conflicts

SortedMap is the interface

The HashSet thread is not safe and does not allow the storage of the same object. Map is used internally to save data, which is to save the HashSet data as the key value of the Map. This is why the elements in the HashSet cannot be repeated, and the key value in the Map will be saved before To determine whether the current Map contains the key object, the internal method is to first pass the hashcode of the key, determine the form hashcode, and then use the equals method to determine whether they are the same.

TreeMap is implemented based on red-black trees, and the key cannot be null.

HashMap and TreeMap are thread unsafe and not synchronized

HashTable and ConcurrentHashMap are thread-safe. HashTable locks the whole and ConcurrentHashMap locks what is needed.

LinkedBlockingQueue is an optional bounded queue that does not allow null values. It is an optional bounded blocking queue based on node links. It is thread-safe, first-in-first-out, and FIFO.

TreeSet sorts naturally, LinkedHashSet sorts in order of addition

PriorityQueue, unbounded queue, thread-safe, does not allow null values, the time complexity of enqueueing and dequeuing is O(logn).

ConcurrentLinkedQueue, thread-safe, follows the FIFO principle.

ConcurrentHashMap is an implementation class of HashMap (thread-safe). The saved values ​​can be repeated, but the keys cannot be repeated.

ConcurrentHashMap uses segments to segment and manage locks. Segments inherit from ReentrantLock.

ConcurrentHashMap uses ReentrantLock to ensure thread safety.

StringBuilder: Thread-unsafe and fastest running

StringBuffer: Thread safe, allows to be faster than String

String is immutable and thread safe

Sting is implemented internally using a char[] array, and char stores unicode codes.

Guess you like

Origin blog.csdn.net/a154555/article/details/126941306