Java programmers often come into contact with collection containers. Today, let's summarize the map Traversal method. The purpose is very simple. You can operate map freely. See the source code: [java] package dec; import java. util. collection; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. map. entry; import java. util. set;/***** <p> * Title: map traversal test class/p> ** <p> * Description: example business class * </p> ** <p> * Copyright: Copyright (c) 2012 * </p> ** @ author dml @ 2012-12-25 * @ version 1.0 */public class MapIterator {public static void main (String [] args) {String [] str = {"I love you", "You love him", "He loves her", "She loves me"}; Map <Integer, string> m = new HashMap (); for (int I = 0; I <str. length; I ++) {m. put (I, str [I]);} System. out. println ("The following is the result output using useKeySet ():"); useKeySet (m); System. out. println ("The following is the result output using the useEntrySet () method:"); useEntrySet (m); System. out. println ("The following is the result output using useValues ():"); useValues (m);}/*** 1. use keySet to traverse ** Set java. util. map. keySet () ** Returns a Set view of the keys contained in this map. the set is backed * by the map, so changes to the map are reflected in the set, and * vice-versa. if the map is modified while an iteration over the set is in * progress (progress T through the iterator's own remove operation), the * results of the iteration are undefined. the set supports element removal, * which removes the corresponding mapping from the map, via the * Iterator. remove, Set. remove, removeAll, retainAll, and clear operations. * It does not support the add or addAll operations. ** Returns: a set view of the keys contained in this map ** @ param m */public static void useKeySet (Map m) {Set s = m. keySet (); Iterator it = s. iterator (); int Key; String value; while (it. hasNext () {Key = (Integer) it. next (); value = (String) m. get (Key); System. out. println (Key + ": \ t" + value) ;}}/*** 2. use entrySet to traverse ** Set java. util. map. entrySet () ** Returns a Set view of the mappings contained in this map. the set is * backed by the map, so changes to the map are reflected in the set, and * vice-versa. if the map is modified while an iteration over the set is in * progress (progress T through the iterator's own remove operation, or through * the setValue operation on a map entry returned by the iterator) the * results of the iteration are undefined. the set supports element removal, * which removes the corresponding mapping from the map, via the * Iterator. remove, Set. remove, removeAll, retainAll and clear operations. * It does not support the add or addAll operations. ** Returns: a set view of the mappings contained in this map ** @ param m */public static void useEntrySet (Map m) {Set s = m. entrySet (); Iterator it = s. iterator (); Map. entry entry; int Key; String value; while (it. hasNext () {entry = (Entry) it. next (); System. out. println (entry. getKey () + ": \ t" + entry. getValue () ;}}/*** 3. use values to traverse ** Collection <String> java. util. map. values () *** Returns a Collection view of the values contained in this map. the * collection is backed by the map, so changes to the map are reflected in * the collection, and vice-versa. if the map is modified while an iteration * over the collection is in progress (progress T through the iterator's own * remove operation), the results of the iteration are undefined. the * collection supports element removal, which removes the corresponding * mapping from the map, via the Iterator. remove, Collection. remove, * removeAll, retainAll and clear operations. it does not support the add or * addAll operations. ** Returns: a collection view of the values contained in this map ** @ param m */public static void useValues (Map <Integer, String> m) {Collection c = m. values (); Iterator it = c. iterator (); while (it. hasNext () {System. out. println (it. next () ;}} execution result: [plain] The following is the result output using the useKeySet () method: 0: I love you 1: You love him 2: he loves her 3: She loves me the following is the result output using the useEntrySet () method: 0: I love you 1: You love him 2: He loves her 3: the following figure shows the output result using useValues (): I love you love him He loves her She loves me