Interview two days ago:
Interviewer: Do you understand containers?
Me: I don’t know very well (inner play: I have only heard of technologies as deep as Docker containers, or I can’t tell directly, so he will continue to ask questions)
Later, I was killed. I wanted to learn about containers today. However, I found that all I found were java collections. The original question was not about Docker containers.
The concept of containers: If there is a class dedicated to storing objects of other classes, this class is called a container, or a collection
blood lessons
So today, I will summarize the common collections (containers) in java:
name | principle | characteristic | thread safety | advantage | shortcoming | |
List
|
ArrayList | Fixed-length array implementation (if the length exceeds the length, a new expansion array will be created and the data will be copied to the new array) | orderly, repeatable | no | fast index | In the case of long distance at the end of the addition and deletion distance, the efficiency is slow |
LinkedList | Use a doubly linked list structure to record the head and tail nodes and the number | orderly, repeatable | no | Add and delete fast | The efficiency is slow when the index is long from the head and tail | |
Map | HashMap | Fixed-length array structure, using Hash to distribute elements, Hash collisions are solved using the zipper method, that is, each element is a singly linked list, and multiple pieces of data with the same hash value are appended backwards | Unordered, Key cannot be repeated | no | Add and delete fast | traversal is slow |
LinkedHashMap | Inherit HashMap and maintain a doubly linked list to ensure ordering | Ordered, Key cannot be repeated | no | Add, delete, search and traverse fast | A doubly linked list is attached to the hashmap, so it is not as efficient as HashMap | |
TreeMap | red-black binary tree structure | ordered (non-additive, sorted by size) | no | fast index |
Addition and deletion efficiency is slow
|
|
CurrentHashMap | 同HashMap | disorder | Yes (use volatile to guarantee concurrent read visibility, use synchronized to guarantee operations) | Add and delete fast | traversal is slow |