你说的map用增强的for循环遍历 是先用keySet() 或entrySet()方法处理 然后for循环遍历- package cn.zl.hashmap;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- /**
- * hashmap的泛型
- * @author Lenovo
- *
- */
- public class MapTest {
- public static void main(String[] args) {
- Map<String,String> map=new HashMap<String,String>();
-
- map.put("a", "aa");
- map.put("b", "bb");
- map.put("c", "cc");
-
- Set<String> set=map.keySet();
- for(Iterator<String> iter=set.iterator();iter.hasNext();)
- {
- String key=iter.next();
- String value=map.get(key);
- System.out.println(key=":"+value);
-
- }
- System.out.println("==========================");
- Set<Map.Entry<String, String>> set2=map.entrySet();
- for(Iterator<Map.Entry<String, String>> iter=set2.iterator();iter.hasNext();)
- {
- Map.Entry<String, String> entry=iter.next();
- String key=entry.getKey();
- String value=entry.getValue();
- System.out.println(key+":"+value);
- }
-
- }
- }
复制代码 |