Map interface
Map is used to store "key (key)-value (value) pairs". The "key-value pairs" stored in the Map class are identified by keys, so "key objects" cannot be repeated.
The implementation classes of the Map interface include HashMap, TreeMap, HashTable, Properties, etc.
HashMap is
implemented using a hash algorithm, and the basic structure of the hash table is "array + linked list". It is the most commonly used implementation class of the Map interface. Since the bottom layer uses a hash table to store data, we require that the key cannot be repeated. If there is a duplication, the new key-value pair will replace the old key-value pair.
HashMap is very efficient in terms of search, deletion, and modification.
Detailed explanation of the underlying
HashTable class
and HashMap usage is almost the same, the underlying implementation is almost the same, except that the HashTable method adds the synchronized keyword to ensure thread synchronization check, which is inefficient.
The difference between HashMap and HashTable:
- HashMap: Thread is not safe and efficient. Allow key or value to be null.
- HashTable: Thread safe, low efficiency. No key or value is allowed to be null
The use of
TreeMap TreeMap and HashMap implement the same interface Map, so there is no difference in usage for the caller. The efficiency of HashMap is higher than that of TreeMap; TreeMap is only used when sorting the Map.
Detailed explanation of the underlying
Java self-study website