List, Set, and Map underlying implementation and use recommendations, setmap
How is List, Set, and Map implemented and stored?
List common implementation methods include ArrayList and rule List.
ArrayList storage method: array, fast Query
Storage Method of the shortlist: linked list, insert, and delete
Common Set implementation methods include HashSet and TreeSet.
HashSet storage method: hash code algorithm. The added object must implement the hashcode () method to quickly search for elements.
TreeSet storage method: stores data in sequence. to store data in sequence, you must implement the Comparable interface.
Additional:
The Collection framework provides two collections classes: collections (sorting, copying, and searching) and Arrays (sorting, copying, and searching)
Common Map implementation methods include HashMap and TreeMap.
HashMap storage method: hash code algorithm for quick search of key values
TreeMap storage: Sort keys
Details:
1: Set
Collection (single column set)
List (ordered, repeatable)
ArrayList
The underlying data structure is an array, which provides fast query and slow addition and deletion.
Low thread security and High Efficiency
Vector
The underlying data structure is an array, which provides fast query and slow addition and deletion.
Thread security and low efficiency
Shortlist
The underlying data structure is a linked list, which is slow in query and fast in addition and deletion.
Low thread security and High Efficiency
Set (unordered, unique)
HashSet
The underlying data structure is a hash table.
The hash table depends on two methods: hashCode () and equals ()
Execution sequence:
First, determine whether the hashCode () value is the same
Yes: continue to execute equals () and check its return value.
True: indicates that the elements are repeated and are not added.
Is false: add it directly to the set.
No: add it directly to the set.
Finally:
Automatically generate hashCode () and equals ()
LinkedHashSet
The underlying data structure consists of a linked list and a hash table.
The linked list ensures the order of elements.
The hash table ensures that the elements are unique.
TreeSet
The underlying data structure is a red/black tree. (A self-balancing Binary Tree)
How can we ensure the uniqueness of elements?
Determines whether the returned value is 0.
How can we ensure the sorting of elements?
Two methods
Natural sorting (comparison of elements)
Implement the Comparable interface for the class to which the element belongs
Comparator sorting (set comparison)
Let the set receive an implementation Class Object of Comparator
Map (double row set)
A: The data structure of the Map set is only valid for keys and has nothing to do with values.
B: stores key-value pairs. The key is unique and the value can be repeated.
HashMap
The underlying data structure is a hash table. Low thread security and High Efficiency
The hash table depends on two methods: hashCode () and equals ()
Execution sequence:
First, determine whether the hashCode () value is the same
Yes: continue to execute equals () and check its return value.
True: indicates that the elements are repeated and are not added.
Is false: add it directly to the set.
No: add it directly to the set.
Finally:
Automatically generate hashCode () and equals ()
LinkedHashMap
The underlying data structure consists of a linked list and a hash table.
The linked list ensures the order of elements.
The hash table ensures that the elements are unique.
Hashtable
The underlying data structure is a hash table. Thread security and low efficiency
The hash table depends on two methods: hashCode () and equals ()
Execution sequence:
First, determine whether the hashCode () value is the same
Yes: continue to execute equals () and check its return value.
True: indicates that the elements are repeated and are not added.
Is false: add it directly to the set.
No: add it directly to the set.
Finally:
Automatically generate hashCode () and equals ()
TreeMap
The underlying data structure is a red/black tree. (A self-balancing Binary Tree)
How can we ensure the uniqueness of elements?
Determines whether the returned value is 0.
How can we ensure the sorting of elements?
Two methods
Natural sorting (comparison of elements)
Implement the Comparable interface for the class to which the element belongs
Comparator sorting (set comparison)
Let the set receive an implementation Class Object of Comparator
2. Collection selection principles
Whether the form is a key-value object:
Yes: Map
Whether the key needs to be sorted:
Yes: TreeMap
No: HashMap
If you do not know, use HashMap.
No: Collection
Unique element:
Yes: Set
Whether the elements need to be sorted:
Yes: TreeSet
No: HashSet
If you do not know, use HashSet.
No: List
To be secure:
Yes: Vector
No: ArrayList or revoke list
Add, delete, and more: Upload list
Multiple queries: ArrayList
If you do not know, use ArrayList.
If you do not know, use ArrayList.
3: common methods and traversal methods of the Set
Collection:
Add ()
Remove ()
Contains ()
Iterator ()
Size ()
Traversal:
Enhanced
Iterator
| -- List
Get ()
Traversal:
Common
| -- Set
Map:
Put ()
Remove ()
Containskey (), containsValue ()
KeySet ()
Get ()
Value ()
EntrySet ()
Size ()
Traversal:
Value search by key
Find the keys and values respectively based on the key-Value Pair object