且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Map遍历方法

更新时间:2022-10-03 14:32:25

Map遍历只要有两种方法:

1.通过Map的KeySet进行遍历

2.通过Map的EntrySet进行遍历


[java] view plaincopy在CODE上查看代码片派生到我的代码片
// Map的遍历方法一:通过map的KeySet进行遍历 
@Test 
public void test4() { 
Map<Integer, String> map = new HashMap<Integer, String>(); 
map.put(1, "good"); 
map.put(2, "morning"); 

Set<Integer> set = map.keySet(); 
for (Integer ky : set) { 
System.out.println(ky + ":" + map.get(ky)); 


System.out.println("-------------------"); 


// Map的遍历方法二:通过map的entrySet进行遍历 
@Test 
public void test5() { 
Map<Integer, String> map = new HashMap<Integer, String>(); 
map.put(1, "good"); 
map.put(2, "morning"); 

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


System.out.println("-------------------"); 


本文转自 netcorner 博客园博客,原文链接:http://www.cnblogs.com/netcorner/p/4209898.html   ,如需转载请自行联系原作者