Map的迭代

Map集合的迭代依赖Map.Entry接口:



 

一个Map的集合可以依靠Map的entrySet()方法把Map集合变成Map.Entry的set集合:



 

成为set后就可以利用加强版for等形式进行迭代,迭代时主要用的方法:



 

例:

package com.wang.test;

import java.util.HashMap;
import java.util.Map;

public class Test3 {

	public static void main(String[] args) {

		Map<Integer, String> map = new HashMap<>();
		map.put(1, "一");
		map.put(2, "二");
		map.put(3, "三");

		for (Map.Entry<Integer, String> entry : map.entrySet()) {
			int key = entry.getKey();
			String value = entry.getValue();
			System.out.println("key : " + key + " value : " + value);
		}

	}
}

 

猜你喜欢

转载自hejiawangjava.iteye.com/blog/2259874