标签:
import java.util.*;
//0 我的Main界面 public class MapTraverse { public static void main(String[] args) { String[] str = {"I love you", "You love he", "He love her", "She love me"}; Map<Integer, String> m = new HashMap(); for(int i=0; i<str.length; i++) { m.put(i, str[i]); } System.out.println("1.使用KeySet遍历,While循环"); MyKetSet(m); System.out.println("2.使用entrySet遍历,While循环"); MyEntrySet(m); System.out.println("3.使用for循环遍历"); MyFor(m); }
// 1 我的MyKetSet()方法 public static void MyKetSet(Map<Integer, String> m) { Set s = (Set<Integer>)m.keySet(); Iterator<Integer> it = s.iterator(); int Key; String value; while(it.hasNext()) { Key = it.next(); value = (String)m.get(Key); System.out.println(Key+":\t"+value); } }
// 2 我的MyEntrySet()方法 public static void MyEntrySet(Map m) { Set s = m.entrySet(); Iterator<Map.Entry<Integer, String>> it = s.iterator(); Map.Entry<Integer, String> entry; int Key; String value; while(it.hasNext()) { entry = it.next(); Key = entry.getKey(); value = entry.getValue(); System.out.println(Key+":\t"+value); } }
// 3 我的MyFor()方法 public static void MyFor(Map<Integer, String> m) { int Key; String value; for(Map.Entry<Integer, String> entry : m.entrySet()) { Key = entry.getKey(); value = entry.getValue(); System.out.println(Key+":\t"+value); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载
标签:
原文地址:http://www.cnblogs.com/XiaoZhiZhang/p/4918475.html