Java collection Another large interface is the map, and collection The biggest difference is that the collection store is an object, and map storage is a pair of objects, that is, the form of key-value storage, key cannot be duplicated. There are 4 classes that can instantiate a map: Hashmap,hashtable,weakhashmap,treemap.
The traversal of map is the first to get his set set through EntrySet (), similarly, KeySet () Gets the set set of his key, values () gets the set set of his value, and iterates through the set collection of iterator.
HashMap
He has all the attributes of map, both key and value can be null. is not thread-safe, if the key is repeated, then the value of the last key will overwrite the value of the previous key, anyway the saved collection, key will not have duplicates. He has an initial capacity (16) and a load factor (default is 0.75), he has a threshold (initial capacity * load factor), and if size exceeds this threshold, then the capacity is X 2.
Hashtable
He feels like HashMap, Hashtable's initial capacity is 11, and each size increases x2 + 1. Both key and value cannot be null for his thread-safe. His traversal sequence and HashMap are reversed, HashMap is the former backwards, and Hashtable is from the back forward. He has a way of traversing, enumeration.
TreeMap
It feels like he's a key with a sequential set of Key-value, and his key can't be null,value can. Nor is he thread-safe.
Weakhashmap
He is similar to HashMap, but his keys are weak keys and will be recycled by GC.
Finally, if you want to turn a thread-unsafe collection into a secure The Collections.synchronizedmap,collections.synchronizedset,collections.synchronizedlist function is a workaround.
A collection of Java comparisons----MAP