Three Map traversal methods and three map Methods
Package decorator; import java. util. collection; import java. util. hashMap; import java. util. map; import java. util. map. entry; import java. util. set; import org. junit. before; import org. junit. test;/*** three Map traversal methods 1. keySet () 2. values () 3. after the entrySet () * method is used to obtain the Set, you can use foreach or iterator instead of, because the data structure determines ** @ author Administrator **/public class MapCycle {Map <Integer, String> map; // prepare the data @ Before public void testData () {map = new HashMap <> (); map. put (1, "lingyi"); map. put (2, "Ling 2"); map. put (3, "Ling 3"); map. put (4, "Ling 4"); map. put (5, "Ling 5");}/** test three methods. These three methods are used to traverse the Set, therefore, you can use foreach or Iterator ** // Method 1: The keySet () method to obtain the Set (key) @ Test public void testFirst () {Set <Integer> set = map. keySet (); for (Integer integer: set) {System. out. println (map. get (integer) ;}// Method 2: Obtain the Collection (value) @ Test public void testSecond () {Collection <String> collection = map. values (); for (String string: collection) {System. out. println (string) ;}// method 3: The entrySet () method obtains the Set <Entry <key, value >>@test public void testThird () {Set <Entry <Integer, String> entries = map. entrySet (); for (Entry <Integer, String> entry: entries) {System. out. println (entry. getValue ());}}}