Map也是属于Collection Framework,但它在hierarchy中是独立的一支。
Collection:单值。
Map:键-值对,no duplicated keys and at most 1 value.
HashMap?
-
类,实现Map接口。
-
HashMap可以用null来做key和value。
-
Not thread-safe as non synchronized。
-
Why hashCode &equals methods are important and should be overridden?
通过计算key的hashCode来find someplace in the hash buckets to store the key-value entry,and LinkedList is used to store the collided keys entry in each bucket.
And equals是用来fetch the correct entry when collision is met in the same bucket.
-
好的key应该具备?
immutable(caching keys), final, with proper defined hashCode and equals methods.
-
HashMap有相应的capacity和load factor,当容量满时,会扩容并rehashing.
Hashtable?
-
类,实现Map接口,同HashMap是一个层次的。
-
Hashtable不接受null。
-
Thread-safe。
HashSet?
- HashSet uses HashMap internally.Objects are stored as keys and dummy Object as values.
Differences?
| null? | synchronized? |
HashMap | y | n |
Hashtable | n | y |
HashSet | y | n |