1. Overview of the functions of the Map collection
(1) Add function
V put (K key,v value): adds an element. Does this actually have another function? Not to tell you, wait to speak
If the key is stored for the first time, the element is stored directly, returning null
If the key does not exist for the first time, replace the previous value with a value and return the previous value
(2) Delete function
void Clear (): removes all key-value pair elements
V Remove (Object key): Deletes the key-value pair element according to the key, and returns the value
(3) Judgment function
Boolean ContainsKey (Object key): Determines whether the collection contains the specified key
Boolean Containsvalue (Object value): Determines whether the collection contains the specified value
Boolean IsEmpty (): Determines whether the collection is empty
(4) Get function
Set<map.entry<k,v>> EntrySet ():???
V get (Object key): Gets the value based on the key
Set<k> KeySet (): Gets the collection of all the keys in the collection
Collection<v> values (): gets a collection of all values in the collection
(5) Length function
int size (): Returns the logarithm of the key-value pairs in the collection
2. Basic function Test:
1 Packagecn.itcast_01;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 6 Public classMapdemo {7 Public Static voidMain (string[] args) {8 //To create a collection object9map<string, string> map =NewHashmap<string, string>();Ten One //adding elements A //V put (K key,v value): Adds an element. Does this actually have another function? Not to tell you, wait to speak - //System.out.println ("Put:" + map.put ("article", "Mai")); - //System.out.println ("Put:" + map.put ("article", "Rotten")); the -Map.put ("Deng Chao", "Sun Li"); -Map.put ("Huang Xiaoming", "Angelababy yang"); -Map.put ("Jay Chou", "Jolin Tsai")); +Map.put ("Jacky Lau Wei", "Yang Mi"); - + //void Clear (): Removes all key-value pair elements A //map.clear (); at - //V Remove (Object key): Deletes the key-value pair element according to the key, and returns the value - //System.out.println ("Remove:" + map.remove ("Huang Xiaoming")); - //System.out.println ("Remove:" + map.remove ("Huang")); - - //boolean containskey (Object key): Determines whether the collection contains the specified key in //System.out.println ("ContainsKey:" + map.containskey ("Huang Xiaoming")); - //System.out.println ("ContainsKey:" + map.containskey ("Huang")); to + //boolean isEmpty (): Determines whether the collection is empty - //System.out.println ("IsEmpty:" +map.isempty ()); the * //int size (): Returns the logarithm of the key-value pairs in the collection $SYSTEM.OUT.PRINTLN ("Size:" +map.size ());Panax Notoginseng - //Output Collection name theSYSTEM.OUT.PRINTLN ("Map:" +map); + } A}
3. Get the functional test:
1 Packagecn.itcast_01;2 3 Importjava.util.Collection;4 ImportJava.util.HashMap;5 ImportJava.util.Map;6 ImportJava.util.Set;7 8 /*9 * Get Features:Ten * V get (Object key): Gets the value according to the key One * set<k> keySet (): Gets The collection of all the keys in the collection A * collection<v> values (): Gets The collection of all values in the collection - */ - Public classMapDemo2 { the Public Static voidMain (string[] args) { - //To create a collection object -map<string, string> map =NewHashmap<string, string>(); - + //creating elements and adding elements -Map.put ("Deng Chao", "Sun Li"); +Map.put ("Huang Xiaoming", "Angelababy yang"); AMap.put ("Jay Chou", "Jolin Tsai")); atMap.put ("Jacky Lau Wei", "Yang Mi"); - - //V get (Object key): Gets the value based on the key -System.out.println ("Get:" + map.get ("Jay Chou"))); -System.out.println ("Get:" + map.get ("Zhou Jie"));//returns null -System.out.println ("----------------------"); in - //set<k> KeySet (): Gets the collection of all the keys in the collection toSet<string> set =Map.keyset (); + for(String key:set) { - System.out.println (key); the } *System.out.println ("----------------------"); $ Panax Notoginseng //collection<v> values (): Gets a collection of all values in the collection -collection<string> con =map.values (); the for(String value:con) { + System.out.println (value); A } the } +}
Run the effect as follows:
Java Basic Knowledge Enhancement Collection Framework note 51:map collection of map sets function overview and testing