java collection framework (detailed explanation)

Collection:
Collection, also known as container, is used to store, extract, and delete data. The collection APIs provided by the JDK are included in the java.util package.

Branches of collections:
Two major branches of the collection framework: Collection interface and Map interface

1. Collection interface:

Insert image description here
Insert image description here

1,List

List represents an ordered and repeatable collection, which can be accessed directly according to the index of the elements
:
Features of List collection
The elements in the collection are allowed to
be repeated The elements in the collection are in order, and the order in which each element is inserted is the order of each element
in the collection Can be accessed or set via index

Commonly used implementation classes of the List interface are: ArrayList, LinkedList, Vector

ArrayList

ArrayList is a dynamic array and our most commonly used collection. It is a typical implementation of the List class.

It allows the insertion of any element that conforms to the rules, even null. Each ArrayList has an initial capacity (10), which represents the size of the array.

As the elements in the container continue to increase, the size of the container will also increase. Each time an element is added to the container, a capacity check will be performed. When it is about to overflow, an expansion operation will be performed.

Therefore, if we know the number of elements to be inserted, it is best to specify an initial capacity value to avoid excessive expansion operations that waste time and efficiency.

ArrayList is good at random access, while ArrayList is asynchronous
:

Vector

Similar to ArrayList, but Vector is synchronized and its operation is almost the same as ArrayList.

LinkedList

LinkedList is implemented using a two-way circular linked list. LinkedList is another implementation of the List interface. In addition to accessing collection elements according to indexes, LinkedList also implements the Deque interface, which can be used as a double-ended queue. That is to say, it can be used as either Used as a "stack", it can also be used as a queue.

2,Set

Set represents an unordered non-repeatable collection and can only be accessed based on the element itself.

Set extends the Collection interface and is an unordered collection that does not allow duplicate elements to be stored.

Commonly used implementation classes of the Set interface are: HashSet, LinkedHashSet, and TreeSet
Insert image description here

3,Queue

Queue is a collection of queues

Queue is an important type of data structure. It supports FIFO, tail addition and head deletion (the elements of the advanced queue are dequeued first), which is similar to the queuing in our lives.
Insert image description here

2. Map interface:

Insert image description here

1.HashMap

The Map interface is based on the implementation of hash tables and is the most frequently used data type for key-value pair processing.

It stores data according to the hashCode value of the key. In most cases, its value can be directly located. It is characterized by fast access speed, uncertain traversal order, thread unsafe, and allows at most one key to be null and multiple values ​​to be null.

You can use the synchronizedMap method of Collections to make HashMap thread-safe, or use the ConcurrentHashMap class.

2.Hashtable

Hashtable and HashMap have many similarities in terms of storage structure and implementation. The difference is that it is inherited from the Dictionary class and is thread-safe. In addition, Hashtable does not allow the key and value to be null, and the concurrency is not as good as ConcurrentHashMap.

Hashtable is not recommended to be used in new code. It can be replaced by HashMap when thread safety is not required. ConcurrentHashMap can be used when thread safety is required.

3.LinkedHashMap

LinkedHashMap inherits HashMap and is a hash table and linked list implementation of the Map interface. It maintains a double linked list. This linked list defines the iteration order, which can be insertion order or access order.

4.TreeMap

TreeMap implements the SortMap interface and can sort the records it saves according to keys. The default is to sort by key values ​​in ascending order (natural order). You can also specify a sorting comparator. When using an Iterator to traverse a TreeMap, the records obtained are sorted. .

5.Map summary

Insert image description here
:
ok. The above is the entire content of the collection framework

Guess you like

Origin blog.csdn.net/sweetser/article/details/134585167