Map set, map
Why use the Map set?
It is not convenient for all elements of the set to judge the object property value. You can obtain the object based on the key. This is not done by the Collection set, and Map can be easily implemented.
What are the features of Map sets?
Objects that map keys to values. A ing cannot contain duplicate keys. Each key can only be mapped to one value at most.
> What is the difference between a Map set and a Collection set?
The stored elements in the Map set appear in pairs. The keys in the Map set are unique and the values are repeated. This can be understood as: couples
The storage elements of the Collection Set appear independently. The Collection sub-Set is unique and the List is repeatable. This can be understood as: Bachelor
Note: The data structure value of the Map set is valid for the key and has nothing to do with the value.
The data structure of the Collection set is effective for elements.
> Map set function Overview:
1: Add function
VPut(K key, V value): Add an element. This actually has another feature.
If the key is stored for the first time, the system directly stores the elements and returns null.
If the key does not exist for the first time, replace the previous value with the value and return the previous value.
2: Deletion
Void clear (): removes all key-value pair elements.
V remove (Object key): deletes a key-Value Pair element based on the key and returns the value
3: Judgment
Boolean containsKey (Object key): determines whether the set contains the specified key.
Boolean containsValue (Object value): determines whether the set contains the specified value.
Boolean isEmpty (): determines whether the set is empty.
4. Retrieval
Set <Map. Entry <K, V>EntrySet(): Get the set of key-value pairs
VGet(Object key): obtains the value based on the key.
Set <K>KeySet(): Obtains the set of all keys in the set.
Collection <V> values (): obtains the set of all values in the Collection.
5: length Function
Int size (): returns the logarithm of the key-value pair in the set.
"Map Traversal method
Map <key type, value type> map = new HashMap <key type, value type> ();
> 1. Key-to-value
Set <key type> set = map.KeySet(); // Obtain the set of all keys for (key type key: set) {value type value = map. get (key); System. out. println (key + "-" + value );} > 2. Key and value together Set <Map. Entry <key type, value type> set = map.EntrySet(); For (Map. entry <key type, value type> entry: set) {key type = entry. getKey (); value Type = entry. getValue (); System. out. println (key + "-" + value );}
Import java. util. hashMap; import java. util. map; public class MapDemo {public static void main (String [] args) {// create a set object Map <String, String> map = new HashMap <String, String> (); // Add element // V put (K key, V value): Add element. Is there another feature? Don't tell you first. out. println ("put:" + map. put ("article", "Ma Yi"); // System. out. println ("put:" + map. put ("article", "Yao Di"); map. put ("Deng Chao", "Sun Li"); map. put ("Huang Xiaoming", "Yang Ying"); map. put ("Jay Chou", "Cai Yilin"); map. put ("Liu kaiwei", "Yang Mi"); // void clear (): removes all key-value pairs. // map. clear (); // V remove (Object key): Delete the key-Value Pair element based on the key and return the value to the System. out. println ("remove:" + map. remove ("Huang Xiaoming"); // System. out. println ("remove:" + map. remove ("Huang Xiaobo"); // boolean containsKey (Object key): determines whether the set contains the specified key. // System. out. println ("containsKey:" + map. containsKey ("Huang Xiaoming"); // System. out. println ("containsKey:" + map. containsKey ("Huang Xiaobo"); // boolean isEmpty (): determines whether the set is empty. // System. out. println ("isEmpty:" + map. isEmpty (); // int size (): returns the logarithm of the key-value pair in the SET System. out. println ("size:" + map. size (); // name of the output set System. out. println ("map:" + map );}}