#1. What is a container
In Java, there is a class specifically used to store objects of other classes , this class is called a container, which is a whole that is formed by combining several class objects with the same or similar properties.
Introduction to several common containers:
Recommended java learning video tutorial: java teaching video
1. List
Ordered collection (also called sequence). Users of this interface have precise control over where each element in the list is inserted. Users can access elements based on their integer index (position in the list) and search for elements in the list. Unlike sets, lists generally allow duplicate elements.
Arraylist: Object array
Vector: Object array
LinkedList: Doubly linked list (circular linked list before JDK1.6, JDK1.7 cancels the cycle)
2. Set
A collection that does not contain duplicate elements. More precisely, set contains no element pair e1 and e2 that satisfies e1.equals(e2) and contains at most one null element. As its name suggests, this interface mimics the mathematical set abstraction.
HashSet (unordered, unique): Based on HashMap, the bottom layer uses HashMap to save elements.
LinkedHashSet: LinkedHashSet inherits from HashSet, and is internally implemented through LinkedHashMap.
LinkedHashMap is implemented internally based on Hashmap, but there are still some differences.
TreeSet (ordered, unique): Red-black tree (self-balancing sorted binary tree).
3. Map
An object that maps keys to values. A map cannot contain duplicate keys; each key can only be mapped to at most one value.
HashMap: Before JDK1.8, HashMap was composed of array and linked list. Array is the main body of HashMap, and linked list mainly exists to resolve hash conflicts ("zipper method" to resolve conflicts). After JDK1.8, there have been major changes in resolving hash conflicts. When the length of the linked list is greater than the threshold (default is 8), the linked list is converted into a red-black tree to reduce search time
LinkedHashMap: LinkedHashMap Inherited from HashMap, so its bottom layer is still based on the zipper hash structure, which is composed of arrays and linked lists or red-black trees.
In addition, LinkedHashMap adds a doubly linked list based on the above structure, so that the above structure can maintain the insertion order of key-value pairs. At the same time, the access sequence related logic is implemented by performing corresponding operations on the linked list.
Hashtable: Array is composed of linked list. Array is the main body of HashMap, and linked list mainly exists to solve hash conflicts.
TreeMap: Red-black tree (self-balancing sorted binary tree)
4. Queue
is used to save the collection of elements before processing them. In addition to basic Collection operations, queues provide additional insertion, extraction, and inspection operations.
Recommended related articles and tutorials: Introduction to java language
The above is the detailed content of What does a container in java mean?. For more information, please follow other related articles on the PHP Chinese website!