A collection of related
List, Set, Map
type | describe |
List | Allows repeated objects, multiple NULL elements can be inserted, in order |
Set | No duplicate objects are allowed, only one null element is allowed, unordered |
Map | It is not a subinterface or implementation class of a Collection. It is an interface. Each element is an entry, with a key and a value |
Vector, ArrayList, LinkedList
type | describe |
Vector | Dynamic array, security, capacity expansion doubled |
ArrayList | Dynamic array, insecure, increased by 50%, initial capacity 10 |
LinkedList | Bidirectional linked lists, insecure, do not need to adjust easily |
Vector: dynamic arrays, secure, doubled capacity increase
ArrayList: dynamic array, insecure, increased by 50%, initial capacity 10
LinkedList: two-way LinkedList, not secure, doesn’t need to be adjusted easily
Why is TreeSet ordered
The SortedSet interface is implemented and a sort order is maintained using a Comparator or Comparable
A HashMap aspects
The internal data structure of the HashMap
Hash table (linked list (O(n))+ array), if the list is too long will be converted to a red-black tree implementation (O(logn))
HashMap tips
knowledge | The answer |
HashMap initial capacity | 16 |
HashMap Expansion increment | Twice the original capacity (2 squared) |
HashMap Adjusts the value of the capacity | Capacity to be adjusted = Current capacity x load factor |
How does a HashMap guarantee randomness | Call the hash function using the hashCode value of the key |
Why is the capacity of HashMap a multiple of 2 | Because of the hash algorithm, let the key’s Hashcode determine the index for maximum randomness |
Why is the capacity of HashMap a multiple of 2 | For maximum randomness, let the key’s hashcode determine the index |
The role of HashCode | Determines the index position of the object in the hash table |
What is a Hash collision? | When different keys use the hash algorithm to locate the storage positions of key/value pairs, the two keys will locate to the same location |
How do I resolve Hash collisions? | Chain address (zipper method) method (i.e. linked list form) |
Why is HashMap thread unsafe | Hashmap does not implement a locking mechanism, and the efficient thread-safe class ConcurrentHashMap has been provided since 1.5 |
Unsafe representation of the HashMap thread | Update loss occurs. The value of B PUT is stored, but the value of A PUT is lost |
HashTable, HashMap, TreeMap, LinkedHashMap
type | Underlying data structure | Whether the synchronization | others |
HashTable | Hash table | Yep | Null keys and null values are not supported |
HashMap | Hash table | Yep | Null keys and null values are supported without order |
TreeMap | Red and black tree | No | Order is determined by the order relationship of keys through the Comparator or by implementing the Comparable interface |
LinkedHashMap | Two-way linked list | No | Traversal order determines order |