map-- Java basic knowledge of common implementation class (hashMap, TreeMap, LinkedHashMap, Hashtable, Properties) and surface operations and related questions

A system of FIG .map
Here Insert Picture Description
two common implementation class Map:
the HashMap : hash table algorithm is employed, then the Map key does not guarantee the order added, does not allow duplicate key.
Key repeat determination criteria: key1 and key2 whether equals true, and hashCode equal.
the TreeMap : red-black tree algorithm is employed, then the Map key be sorted according to the natural order or custom ordering, duplicate key is not allowed.
key repeat determination criteria: compareTo / compare the whether the return value of 0. the
a LinkedHashMap : linked list algorithm and hash tables, then the Map key will ensure that the order has added, allowed to repeat key.
the same judgment is repeated key standards and criteria in the HashMap key.
the hashtable : the use of hash table algorithm, is the predecessor of HashMap (Vector is similar to ArrayList's predecessor) ...
prior to the Java collections framework, a map showing the relationship on the use of Hashtable.
All methods use synchronized modifier, thread-safe, but the relative performance lower HashMap.
the Properties : Hashtable subclass, which requires that key and value are of type String.
to load the resource file (properties file).
in general, we define Map, ke y are immutable classes (String), the key name as a unique value.
distinction:
HashMap and TreeMap and LinkedHashMap threads are unsafe, but high performance:
Solution: Map m = Collections.synchronizedMap (Map objects);
Hashtable class implements thread-safe, but lower performance.
Hash table algorithm: do etc. the fastest query value.
number structure algorithms: do range queries fastest -> applied to the index.

Three common interview questions:.
(1) the Set and Map What is the difference?
Set contains a value only, while Map contains the keys and values.
Set contains a unique value, and Map can contain a unique key with duplicate values.
Set the number of stored single null value, and the n Map may comprise a null value and a null key.

(2) HashSet and HashMap What is the difference?
HashSet contains values only, and HashMap contains entries (key, value). HashSet iteration may, but need to convert HashMap to Set iteration. HashSet implements Set interface, HashMap implements Map interface. HashSet not contain any duplicate values, and may contain duplicate values HashMap with a unique key. HashSet can contain only null value, and HashMap may contain a null n

(3) HashMap and TreeMap What is the difference?
HashMap sequence is not maintained, but maintained TreeMap ascending. HashMap achieved by a hash table, which is implemented by TreeMap tree structure. HashMap may sort key or value, but may TreeMap sort key. HashMap may contain a null value having a plurality of keys and a null, and a null key TreeMap not contain a null value, but may have a plurality.

(4) hashmap the bottom of the list is an array +, arrays are used to doing list is used to doing??
Array: storage interval in a row, take a serious memory addressing is easy to insert delete difficulties
list: storage interval discrete occupancy memory is relatively loose, addressing the difficulties, easy to insert delete
hashmap integrated application of these two data structures, addressing achieve easy, too easy insertion deleted

(5) inside a class hashmap back to what caused the problem?
If you direct your custom class objects stored in the above map, the map will then your object type Object objects stored, when need, It can be cast to the type of class you need
this method when you put this object is not defined you will be given.
Solution: Or you can use the generic definition of HashMap

Published 99 original articles · won praise 2 · Views 2601

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105310740