The map set can be retrieved in two ways,
1. keyset: store keys in the map into the set, and use the set iterator to process all keys.
The sample code is as follows:
Import Java. util. *; Class test {public static void main (string [] ARGs) {Map <string, integer> map = new hashmap <string, integer> (); map. put ("fan", 23); map. put ("peng", 45); map. put ("Cheng", 34); // gets the set <string> keyset = map. keyset (); iterator <string> it = keyset. iterator (); While (it. hasnext () {string keystring = it. next (); system. out. println (keystring + "-" + map. get (keystring ));}}}
2. entryset
The key-value relationship in the Key Map set is returned in the form of a set, and then the set iterator is used
Format: Set <map. Entry <K, V>
The Code is as follows:
Class test {public static void main (string [] ARGs) {Map <string, string> map = new hashmap <string, string> (); map. put ("fan", "fan"); map. put ("peng", "peng"); map. put ("Cheng", "Cheng"); // nested form of generics. The relationship is map. entry <K, V> type set <map. entry <string, string> entryset = map. entryset (); iterator <map. entry <string, string> it = entryset. iterator (); While (it. hasnext () {map. entry <string, string> entry = it. next (); string key = entry. getkey (); string value = entry. getvalue (); system. out. println (Key + "-" + value );}}}
Two methods to retrieve a map set