Talking about Collection Framework in Java

The core interfaces of the Java collection framework are java.util.Collection and java.util.Map. Each collection framework is based on the difference in the underlying data structure and endowed with different characteristics, so as long as you understand the characteristics of the underlying data structure, you can Have a good grasp of the characteristics and application of the collection. Let's talk about the different collections and their characteristics

**

Interface List, Set and Queue under Collection

**
List Features: Orderly, repeatable
Implementation class:
arraylist
array represents the array data structure used at the bottom layer and is stored by subscript. Everything is in order. Query efficiency is O(1)
ordered: for example

List<Integer> list = new ArrayList<>();
list.add(3);
list.add(2);
list.add(1);
System.out.println(list);
输出;[3, 2, 1]

linkedlist linked means that the underlying linked list is also ordered, and the ordered linked list is discontinuous, so it cannot be queried by subscript, and the query efficiency is O(n)

Set feature: the set feature is unordered and non-repetitive (the data structure features of hash and tree will cause it to be unordered)
implementation class:

HashSet hash represents the hash table data structure, which is stored with the hashcode modulus of the object as the subscript. Because the hashcode of the object is uncertain, all the moduli are not necessarily the same, resulting in disorder. For example
:

HashSet<Integer> integers = new HashSet<>();
integers.add(3);
integers.add(2);
integers.add(1);
System.out.println(integers);
输出 :[1, 2, 3]

TreeSet tree means that the bottom layer is a binary tree (red-black tree, which provides an ordered collection), which can be sorted (positive order or reverse order, the default is from small to large), so it is also unordered storage 1, 3, 2 is taken out to be 1, 2, 3

TreeSet<Integer> set = new TreeSet<>();
set.add(3);
set.add(1);
set.add(2);

System.out.println(set); // 输出: [1, 2, 3]

Queue feature: Queue

Implementation class:
priorityqueue priority queue, the bottom layer is a heap (a kind of binary tree) (the heap is divided into a large top heap and a small top heap (the largest and smallest are always the first)), so it is also an unordered deque
. Data structures of the nature of queues and stacks. Elements in the double-ended queue can be popped from both ends
. Common method:
create a new double-ended queue: Deque deque = new LinkedList();
determine whether it is empty: deque.isEmpty()
increase: deque.offerFirst(), deque.offerLast( );//Add from the beginning and add to the end Delete
: deque.pollFirst(), deque.pollLast()//Add from the beginning and dequeue from the end Check
: deque.peekFirst(), deque.peekLast()//Check the addition and tail of the head element

Map (k, v) key-value
pair implementation class:
hashmap hash represents the hash table data structure, which is stored in (array, linked list, red-black tree) with the hashcode modulo of the object as the subscript, and also belongs to the unordered storage hashtable (
for hashmap lock)
treeMap tree means that the bottom layer is a binary tree (binary search tree red-black tree) map means mapping (k, value), so keys must be sortable

Question: With a binary search tree, why is there a binary heap? For example, the treemap is sorted, why do
we need a priority queue
? The fork sorting tree is balanced, and the height n of the binary sorting tree of n nodes has a search efficiency of logn, which is approximately a half search. If the binary sorting tree is completely unbalanced, its depth can reach n, and the search efficiency is O(n). The same is true for degenerating into sequential search and insertion. To find the insertion place, the heap insertion time complexity is logn, but take the largest and smallest The time complexity is O(1).

Guess you like

Origin blog.csdn.net/weixin_43866043/article/details/130945490