/*
* Two ways to remove a map collection: (Map without iterator method)
* 1, set<k> KeySet: All the keys in the map are deposited into the set set because the set has iterators.
* So you can iterate through all the keys, and then according to the Get method, get each key corresponding value.
* The extraction principle of the Map collection: The Map collection is transferred to the set set and then removed by the iterator
* 2, Set<map.entry<k,v>> EntrySet: Stores the mappings in the map collection into the set set,
* and the data type of the relationship is: map.entry
*/
Package Map overview; import java.util.hashmap;import java.util.iterator;import java.util.map;import java.util.set;/* * Map.entry actually Entry is also an interface, it is an internal interface in the Map interface *//*interface map{public static interface Entry{public abstract Object GetKey () ;p ublic abstract Object getValue ();}} Class HashMap implements Map.entry{class HaHa implements map.entry{@Overridepublic Object GetKey () {return null;} @Overridepublic Object GetValue () {return null;}} @Override//public Object GetKey () {//return null;//}////@Override//public Object GetValue () {//return null;//}}*/ public class Test {public static void main (string[] args) {map<string, string> Map = new hashmap<string, STRING&G t; (); Map.put ("zhangsan2"), Map.put ("The", "Zhangsan3"), Map.put ("", "Zhangsan1"), Map.put ("A", "Zhangsan4"); /Method 1: Take out the mappings in the Map collection, deposit into the set set set<map.entry<string, string>> entryset = Map.entryset ();iterator< Map.entry<string, string>> it = Entryset.iterator (); while (It.hasnext ()) {map.entry<string, String> me = It.next ();//After the relationship object Map.entry is acquired, the key and value in the relationship can be obtained by Map.entry//getkey and GetValue methods string key = Me.getkey (); String value = Me.getvalue (); System.out.println ("Key:" +key+ "--" + "value:" +value);} Method 2: Get the set set of all keys for the map collection first, KeySet (). set<string> KeySet = Map.keyset ();//The iterator can be obtained with the set set. Iterator<string> it2 = Keyset.iterator (); while (It2.hasnext ()) {String key = It2.next ();//With Key Its corresponding value can be obtained through the Get method of the map collection. String value = Map.get (key); System.out.println ("key:" + key + "-+" + value);}}}
Two ways to remove a map collection (KeySet, EntrySet)