Map collection
Collection Interface: defines a single set of specifications Collection <E>
- A single storage element per element
Map interface: defines a set of specifications of the double row Map <K, V>
- One pair each storage element
java.util.Map <K, V> set
Map of feature set:
- Map is set a double-row set , an element contains two values (a key, a value)
- Map elements of the collection, key and value data type may be the same or different
- Map elements of the collection, Key is not allowed to repeat, value can be repeated
- Map elements in the collection, key and value is one to one
java.util.HashMap <K, V> collection implements Map <K, V> Interface
HashMap set of features:
1.HashMap collection bottom is a hash value: special fast query
- Before JDK.8: singly linked list arrays +
- After JDK.8: Array + singly linked list / black trees (chain length of more than 8) : increase the speed of the query
2.HashMap set is an unordered collection, storage and order of the elements may be inconsistent extraction elements
java.util.LinkedHashMap <k, v> set extends HashMap <K, V> set
LinkedHashMap features:
- The bottom layer is set LinkedHashMap hash table + chain (guaranteed iteration order)
- LinkedHashMap collection is a ordered collection , storage and order of the elements of the extraction elements is consistent with
Map interface common method
public V put (K key, V value): adding the specified value of the key to the Map specified collection.
Return value: v
- When the key-value pairs stored, key will not be repeated, the return value is null V
- When stored key-value pairs, key duplication, the new value will replace the duplicate value map, the return value is replaced by the value
public V remove (Object key): the value corresponding to the specified key to delete the set of elements in the Map, the return value of the element is deleted.
Return value: V
- key presence, v return value is deleted
- key does not exist, v returns null
V GET public (Object Key) , acquires a value corresponding to the set in Map with a specified key.
return value:
- key is present, the value corresponding to the return value
- key does not exist, return null
boolean containsKey (Object key) determines whether the collection contains the specified key
- Contains returns true, it does not include returns false
The first traversal Map collection: find a way through the key value
Map collection methods:
- Set <K> keySet () Returns a Set view of the keys contained in this map.
Implementation steps:
- Use Map collection keySet (), the set of all the Map key taken out, stored in a Set collection.
- Set traverse the collection , get Map in the collection of each Key
- By Map collection methods get (key), pass through to find the key value
Entry key object
Map.Entry <K, V>: Map interface has one internal Entry Interface
effect: when creating a collection Map, it will create a Map Entry object in the collection, and to record the key value (key object, mapping between keys and values)
The second approach Map traverse the set: Use the Entry Object traversal
Set <Map.Entry <K, V> entrySet () Returns a Set view of the keys contained in this map.
Implementation steps:
- Set <Map.Entry <K, V >> entrySet (): the plurality of objects inside the Map Entry taken out of storage to a set of collection Set
- Traversal Set Collection: get every Enter Object Set collection
- Get Entry object Method: getKey () Gets key, getValue () Get value
Custom type key storage HashMap
Key: the Person type
- Person type must override hashCode method and the equals method to ensure a unique key
value: String type
- Can repeat
java.util.linkedHashMap<K,V> extends HashMap<K,V>
Hash table and linked list a list of the Map interface, with a predictable iteration order.
The underlying principle:
- + Chain hash table (sequential recording element)
Hashtable collection
java.util.Hashtable <K, V> collection implements Map <k, V> Interface
- Hashtable: the bottom is a hash table, is a collection of thread-safe, is a collection of single-threaded, slow
- HashMap: the bottom is a hash table, is a thread safe collection is a collection of multi-threaded, high speed
- HashMap set (the set of all previous studies): store a null value, null key
- Hashtable collection: can not store a null value, null key
- Hashtable and Vector collection of collections as being more advanced collection (HashMap, ArrayList) replaced after jdk1.2 version
- Hashtable subclass properties are still active in the arena calendar
- Properties and is a unique set of I / O stream combined set