class="java" name="code">package com.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * 遍历: key value * */ public class KeySetDemo { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("01", "zhangsan1"); map.put("02", "zhangsan2"); map.put("03", "zhangsan3"); map.put("04", "zhangsan4"); Set<String> keySet = map.keySet(); //方法1: for (String key : keySet) { System.out.println(key+"--->"+map.get(key)); } //方法2: for( String key :map.keySet()){ System.out.println(key+"-------"+map.get(key)); } //方法3: Iterator<String> it = keySet.iterator(); while(it.hasNext()){ String key = it.next(); String value = map.get(key); System.out.println(key+" " +value); } } }