Corresponding key and value in Map:
"Dagu", "diga Altman"
"Mountain", "Gaia Altman"
"Li Xiangyang", "emperor Xia"
4. Objects to create the Map collection:
Polymorphic way
The specific implementation classes are HashMap, HashTable, ConcurrentHashMap, TreeMap, LinkedHashMap, weakHashMap... (say something commonly used)
5. All methods:
-
- |Modifier and Type | method description|
| --- | --- | --- |
| `void` | `[clear]( )()` |
Optionally, delete all mappings from the map.
|
| `default [V]( )` | `[compute]( )([K]( ) key, [BiFunction]( )<? super [K]( ),? super [V]( ),? extends [V]( )> remappingFunction)` |
Attempts to calculate the mapping for the specified key and its current mapping value (if there is no current mapping, then `null` ).
|
| `default [V]( )` | `[computeIfAbsent]( )([K]( ) key, [Function]( )<? super [K]( ),? extends [V]( )> mappingFunction)` |
If the specified key is not already associated with (or mapped to) a value `null` ),Then try to calculate its value using the given mapping function and enter it into this mapping unless it is `null` .
|
| `default [V]( )` | `[computeIfPresent]( )([K]( ) key, [BiFunction]( )<? super [K]( ),? super [V]( ),? extends [V]( )> remappingFunction)` |
If the value of the specified key exists and is not empty, an attempt is made to calculate a new mapping for the given key and its current mapping value.
|
| `boolean` | `[containsKey]( )([Object]( ) key)` |
Returns if this mapping contains a mapping for the specified key `true` .
|
| `boolean` | `[containsValue]( )([Object]( ) value)` |
Returns if the mapping maps one or more keys to the specified value `true` .
|
| `static <K,V> [Map.Entry]( )<K,V>` | `[entry]( )(K k, V v)` |
Returns an immutable containing the given key and value[Map.Entry]( ) .
|
| `[Set]( )<[Map.Entry]( )<[K]( ),[V]( )>>` | `[entrySet]( )()` |
Returns the of the mappings contained in this map[Set]( )View.
|
| `boolean` | `[equals]( )([Object]( ) o)` |
Compares the specified object with this mapping for equality.
|
| `default void` | `[forEach]( )([BiConsumer]( )<? super [K]( ),? super [V]( )> action)` |
Perform the given operation on each entry in this map until all entries are processed or the operation throws an exception.
|
| `[V]( )` | `[get]( )([Object]( ) key)` |
Returns the value to which the specified key is mapped, or if the mapping does not contain the mapping of the key `null` .
|
| `default [V]( )` | `[getOrDefault]( )([Object]( ) key, [V]( ) defaultValue)` |
Returns the value to which the specified key is mapped, or if the mapping does not contain the mapping of the key `defaultValue` .
|
| `int` | `[hashCode]( )()` |
Returns the hash code value for this map.
|
| `boolean` | `[isEmpty]( )()` |
Returns if the mapping does not contain a key value mapping `true` .
|
| `[Set]( )<[K]( )>` | `[keySet]( )()` |
Returns the of the keys contained in this map[Set]( )View.
|
| `default [V]( )` | `[merge]( )([K]( ) key, [V]( ) value, [BiFunction]( )<? super [V]( ),? super [V]( ),? extends [V]( )> remappingFunction)` |
If the specified key is not already associated with a value or null Associated with a given non null value.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )()` |
Returns an immutable map with zero mapping.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1)` |
Returns an immutable map that contains a single map.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2)` |
Returns an immutable map containing two mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2, K k3, V v3)` |
Returns an immutable map containing three mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)` |
Returns an immutable map containing four mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)` |
Returns an immutable map containing five mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)` |
Returns an immutable map containing six mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)` |
Returns an immutable map containing seven mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)` |
Returns an immutable map containing eight mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)` |
Returns an immutable map containing nine mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[of]( )(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)` |
Returns an immutable map containing ten mappings.
|
| `static <K,V> [Map]( )<K,V>` | `[ofEntries]( )([Map.Entry]( )<? extends K,? extends V>... entries)` |
Returns an immutable map containing keys and values extracted from a given entry.
|
| `[V]( )` | `[put]( )([K]( ) key, [V]( ) value)` |
Optionally, associate the specified value with the specified key in the map.
|
| `void` | `[putAll]( )([Map]( )<? extends [K]( ),? extends [V]( )> m)` |
Optionally, copy all mappings for the specified map to this map.
|
| `default [V]( )` | `[putIfAbsent]( )([K]( ) key, [V]( ) value)` |
If the specified key is not already associated with (or mapped to) a value `null` )Associate it with the given value and return `null` ,Otherwise, the current value is returned.
[First tier big factory Java Analysis of interview questions+Back end development learning notes+Latest architecture explanation video+[handout of actual project source code] Browser open: qq.cn.hn/FTf Free collection
|
| `[V]( )` | `[remove]( )([Object]( ) key)` |
If it exists (from an optional operation), delete a key mapping from the map.
|
| `default boolean` | `[remove]( )([Object]( ) key, [Object]( ) value)` |
Deletes the entry only if the specified key is currently mapped to the specified value.
|
| `default [V]( )` | `[replace]( )([K]( ) key, [V]( ) value)` |
The entry for the specified key can be replaced only when the target is mapped to a value.
|
| `default boolean` | `[replace]( )([K]( ) key, [V]( ) oldValue, [V]( ) newValue)` |
The entry for the specified key can be replaced only if it is currently mapped to the specified value.
|
| `default void` | `[replaceAll]( )([BiFunction]( )<? super [K]( ),? super [V]( ),? extends [V]( )> function)` |
Replace the value of each entry with the result of calling the given function on that entry until all entries are processed or the function throws an exception.
|
| `int` | `[size]( )()` |
Returns the number of key value mappings in this map.
|
| `[Collection]( )<[V]( )>` | `[values]( )()` |
Returns the of the values contained in this map[Collection]( )View.
|
6. Map considerations:
- The Map interface provides three set views that allow the contents of the Map to be regarded as a set of keys, values or key value mapping sets. The order of the Map is defined as the order in which the set views on the Map iteratively return elements. Some Map implementations, such as the TreeMap class, make specific guarantees for their order; others, such as the HashMap class, do not.
Note: if you use a variable object as a map key, you must be very careful. If the value of the object affects`equals`If the comparison method is changed and the object is a key in the map, the mapping behavior will not be specified. A special case of this prohibition is that the map is not allowed to include itself. Although the map can take itself as a value, it is recommended that you be very careful: `equals`and`hashCode`The method is not clear on such a map.
All generic mapping implementation classes should provide two "standard" constructors: one that creates an empty mapping void((no arguments) constructor and one with type`Map`The constructor of a single parameter, which creates a new mapping with the same key value as the parameter. In fact, the latter constructor allows the user to copy any map and produce an equivalent map of the required class. There is no way to enforce this recommendation (because the interface cannot contain a constructor), and JDK All common mapping implementations in meet the requirements.
If this mapping does not support this operation, the "destructive" methods contained in this interface (that is, the methods that modify the mapping of its operation) will be specified as thrown`UnsupportedOperationException` . If this is the case, these methods may, but do not have to, be thrown if the call has no impact on the map`UnsupportedOperationException` . For example, call on a map that cannot be modified[putAll(Map)]( )")Method may map it to a "superimposed" map blank, but it does not have to throw an exception.
Some map implementations have restrictions on the keys and values they may contain. For example, some implementations prohibit empty keys and values, and some have restrictions on the type of keys. Attempting to insert unqualified keys or values will throw an unchecked exception, usually`NullPointerException`or`ClassCastException` . An attempt to query for the existence of an unqualified key or value may throw an exception, or it may just return false; Some implementations will show the behavior of the former, while others will show the behavior of the latter. More generally, an attempt to operate on a key or value that does not meet the conditions will not result in the insertion of an unqualified element into the map, which may lead to an exception or may successfully execute the option. This exception is marked as "optional" in the specification of this interface.
Collections Framework Many methods in the interface follow[equals]( )Method. For example, [containsKey(Object key)]( )")The specification of the method says, "if and only if this mapping contains`k`Mapping relationship of`true`Return only when`(key==null ? k==null : key.equals(k))` . This specification_no_Should be interpreted to mean calling`Map.containsKey`And non null parameters`key`Will cause`key.equals(k)`Any key called`k` . Implementations are free to implement optimizations to avoid`equals`Call, for example, first compare the hash codes of two keys( [Object.hashCode()]( )")The specification guarantees that two objects with unequal hash codes cannot be equal Collections Framework The implementation of the interface can make use of the underlying[Object]( )The specified behavior of the method, whether the performer deems it appropriate.
Some map operations that recursively traverse the map may fail, and the map contains its own self referenced instances directly or indirectly. This includes`clone()` , `equals()` , `hashCode()`and`toString()`method. Implementations can optionally handle self referencing scenarios, but most current implementations do not.
2, HashMap (let's take a look at the goblins in the Map Nebula)
======================================
1. Gaia!!! (nothing is boring...)... Overview:
"Sorry... I'm not bored!!"
- HashMap is a Map interface based on the implementation of hash table. This implementation provides all optional mapping operations and allows null values and null keys. (the HashMap class is roughly equivalent to Hashtable, except that it is asynchronous and allows null). This class does not guarantee the order of the Map; In particular, it cannot guarantee that the order will remain unchanged for a period of time.
HashMap is a hash table that stores key value mappings.
HashMap implements the Map interface, stores data according to the HashCode value of the key, has fast access speed, allows the key of one record to be null at most, and does not support thread synchronization.
HashMap is unordered, that is, the insertion order is not recorded.
HashMap inherits from AbstractMap and implements Map, Cloneable and java.io.Serializable interfaces.
"As long as I'm strong enough, it's no use who comes! The Map nebula is mine."
"Come on, pretend, come on, be happy."
2. Unfortunately, I'm not my Di, ha ha, Dawei Tianlong!!! (see magic weapon):
-
- |Modifier and Type | method description|
| --- | --- | --- |
| `void` | `[clear]( )()` |
Remove all mappings from this map.
|
| `[Object]( )` | `[clone]( )()` |
Return to this `HashMap`Shallow copy of the instance: keys and values themselves are not cloned.
|
| `[V]( )` | `[compute]( )([K]( ) key, [BiFunction]( )<? super [K]( ),? super [V]( ),? extends [V]( )> remappingFunction)` |
Attempts to calculate the mapping of the specified key and its current mapping value (if there is no current mapping, then `null` ).
|
| `[V]( )` | `[computeIfAbsent]( )([K]( ) key, [Function]( )<? super [K]( ),? extends [V]( )> mappingFunction)` |
If the specified key is not already associated with (or mapped to) a value `null` ),Then try to calculate its value using the given mapping function and enter it into this mapping unless `null` .
|
| `[V]( )` | `[computeIfPresent]( )([K]( ) key, [BiFunction]( )<? super [K]( ),? super [V]( ),? extends [V]( )> remappingFunction)` |
If the value of the specified key exists and is not empty, an attempt is made to calculate a new mapping for the given key and its current mapping value.
|
| `boolean` | `[containsKey]( )([Object]( ) key)` |
Returns if this mapping contains a mapping for the specified key `true` .
|
| `boolean` | `[containsValue]( )([Object]( ) value)` |
Returns if the mapping maps one or more keys to the specified value `true` .
|
| `[Set]( )<[Map.Entry]( )<[K]( ),[V]( )>>` | `[entrySet]( )()` |
Returns the of the mappings contained in this map[Set]( )View.
|
| `void` | `[forEach]( )([BiConsumer]( )<? super [K]( ),? super [V]( )> action)` |
Perform the given operation on each entry in this map until all entries are processed or the operation throws an exception.
|
| `[V]( )` | `[get]( )([Object]( ) key)` |
Returns the value to which the specified key is mapped, or if the mapping does not contain a key mapping `null` .
|
| `[V]( )` | `[getOrDefault]( )([Object]( ) key, [V]( ) defaultValue)` |
Returns the value to which the specified key is mapped, or if the mapping does not contain the mapping of the key `defaultValue` .
|
| `boolean` | `[isEmpty]( )()` |
Returns if the mapping does not contain a key value mapping `true` .
|
| `[Set]( )<[K]( )>` | `[keySet]( )()` |
Returns the of the keys contained in this map[Set]( )View.
|
| `[V]( )` | `[merge]( )([K]( ) key, [V]( ) value, [BiFunction]( )<? super [V]( ),? super [V]( ),? extends [V]( )> remappingFunction)` |
If the specified key is not already associated with a value or null Associated with a given non null value.
|
| `[V]( )` | `[put]( )([K]( ) key, [V]( ) value)` |
Associates the specified value with the specified key in this mapping.
|
| `void` | `[putAll]( )([Map]( )<? extends [K]( ),? extends [V]( )> m)` |
Copies all mappings of the specified map to this map.
|
| `[V]( )` | `[putIfAbsent]( )([K]( ) key, [V]( ) value)` |
If the specified key is not already associated with (or mapped to) a value `null` )Associate it with the given value and return `null` ,Otherwise, the current value is returned.
|
| `[V]( )` | `[remove]( )([Object]( ) key)` |
Removes the mapping for the specified key, if any, from the map.
|
| `boolean` | `[remove]( )([Object]( ) key, [Object]( ) value)` |
Deletes the entry only if the specified key is currently mapped to the specified value.
|
| `[V]( )` | `[replace]( )([K]( ) key, [V]( ) value)` |
The entry for the specified key can be replaced only when the target is mapped to a value.
|
| `boolean` | `[replace]( )([K]( ) key, [V]( ) oldValue, [V]( ) newValue)` |
The entry for the specified key can be replaced only if it is currently mapped to the specified value.
|
| `void` | `[replaceAll]( )([BiFunction]( )<? super [K]( ),? super [V]( ),? extends [V]( )> function)` |
Replace the value of each entry with the result of calling the given function on that entry until all entries are processed or the function throws an exception.
|
| `int` | `[size]( )()` |
Returns the number of key value mappings in this map.
|
| `[Collection]( )<[V]( )>` | `[values]( )()` |
Returns the of the values contained in this map[Collection]( )View.
|
3. It's just that I paid by mistake after all. I'm still tempted by the spicy bird. I really gave too much. So, pay attention. I knew these things early, early... Happy! (pay attention):
The key and value types of HashMap can be String type or wrapper type of other data
The addition of the elements of the Map Collection is not the add () method of the Collection collection, but the Put() method.
Map traversal is no longer a for loop or a while loop to iterate over integer, but uses two new traversal methods, through keySet or entrySet.
But after all, nightmares will come, everything beautiful will be lost, and it's no use crying:
4. Method code example, let's call the light and purify everything ugly:
Let's take a holistic view and test these methods.
Define a variable of Map interface type, reference an implementation class, and add key value pairs,
Judge whether the set contains a key value, and obtain the value value through a key value,
Delete key value pairs through a key,
Add another map set to this map set,
Judge whether it is empty,
Clear the collection,
Return the number of elements in the set and other common operations (return a length problem, think of a way by yourself, and count it for me (three tones)!).
Traverse the map set in the above question in two ways.
(code below)
package MapDemo;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Map001 {
public static void main(String[] args) {
//Create a Map collection
Map<String,String> map=new HashMap<String, String>();
//Save data, put method
map.put("LAOZI", "Tao Te Ching");
map.put("KONGZI", "the Analects");
map.put("ZHUANGZI", "Nanhua Sutra");
map.put("GEHONG", "baopuzi");
//Find the value according to the key
String s = map.get("LAOZI");
System.out.println(s);
System.out.println("--------------");
//Delete the value value according to the key
String s1 = map.remove("GEHONG");
System.out.println(s1);
System.out.println("--------------");
Map<String,String> map1=new HashMap<>();
map1.put("wuming", "ten thousand swords belong to the sect");
//Global storage: one double column set is stored in another double column set
map.putAll(map1);
//Determine whether the collection is empty
boolean empty = map.isEmpty();
System.out.println(empty);
System.out.println("-------------");
//entrySet of traversal method
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> me:entrySet){
String key= me.getKey();
String value=me.getValue();
System.out.println(key+","+value);
}
System.out.println("------------");
//Clear the data in the collection
map.clear();
//keySet of traversal method