Java中遍历Map的几种方法

方法分为两类:

一类是基于map的Entry;map.entrySet();

一类是基于map的key;map.keySet()

而每一类都有两种遍历方式:

a.利用迭代器 iterator;

b.利用for-each循环;

 

代码举例如下

 

package cn.wzb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

//循环遍历map的方法
public class TestMap {
	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("a", 1);
		map.put("b", 2);
		map.put("c", 3);
		
		// JDK1.4中
		// 遍历方法一 hashMap.entrySet() 遍历
		System.out.println("方法一");
		Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Integer> entry =  it.next();
			String key = entry.getKey();
			 Integer value = entry.getValue();
			System.out.println("key = " + key + "; value = " + value);
		}
		
		System.out.println("");
		
		// JDK1.5中,应用新特性For-Each循环	// 遍历方法二
		System.out.println("方法二");
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue().toString();
			System.out.println("key =" + key + " value = " + value);
		}
		System.out.println("");
		
		// 遍历方法三 hashMap.keySet().iterator() 遍历
		System.out.println("方法三");
		for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
			 String key = i.next();			
			System.out.println(" key = " + key + "; value = " + map.get(key));
		}
		
	   /*
		for (Iterator<Integer> i = map.values().iterator(); i.hasNext(); ) {
			Integer value = i.next();
			System.out.println(value));// 循环输出value
		}
		*/
		/*
		for(Integer value : map.values() ) {
			System.out.println("value = " + value);
		}
		*/
		
		
		System.out.println("");
		// 遍历方法四  Hashmap.keySet()遍历
		System.out.println("方法四");
		for (String key : map.keySet()) {
			System.out.println("key = " + key + "; value = " + map.get(key));
		}
		System.out.println();
		
		// java如何遍历Map <String, ArrayList> map = new HashMap <String, ArrayList<Integer> >();
		System.out.println("java  遍历Map <String, ArrayList<Integer> map = new HashMap <String, ArrayList<Integer>>();");
		System.out.println("方法一:");
		Map<String, List<Integer> > mapList  = new HashMap <String, List<Integer> >();
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list.add(5);
		mapList.put("aaa", list);
		mapList.put("bbb", list);
		
		Set<String> keys = mapList.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			System.out.println(key + " : ");
			
			List<Integer> arrayList = mapList.get(key);
			for (Integer i : arrayList) {
				System.out.print(i + ", ");
			}
			System.out.println();
		}
		
		System.out.println("方法二:");
		
		for (Map.Entry<String, List<Integer> > entry : mapList.entrySet()) {
			String key = entry.getKey();
			System.out.println(key + " : ");
			
			List<Integer> values = (List<Integer>) entry.getValue();
			for (Integer value : values) {
				System.out.print(value + ", ");
			}
			System.out.println();
		}
	}
}

  

posted @ 2015-12-25 21:48  coderbinbin  阅读(321)  评论(0编辑  收藏  举报