package com.itcast.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* map遍历
* @author wangzewen
*
*/
public class MapErgodic {
/**
* 通过键集合的迭代器
* @param map
*/
public static void method1(Map<String,String> map){
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
String value = map.get(key);
System.out.println(key+"***"+value);
}
}
/**
* 通过键的集合
* @param map
*/
public static void method2(Map<String,String> map){
Set<String> keySet = map.keySet();
for (String str : keySet) {
System.out.println(str+"###"+map.get(str));
}
}
/**
* 通过map实体,迭代器
* @param map
*/
public static void method3(Map<String,String> map){
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while(it.hasNext()){
Entry<String, String> next = it.next();
System.out.println(next.getKey()+"^^^"+next.getValue());
}
}
/**
* 通过遍历map实体集合
* @param map
*/
public static void method4(Map<String,String> map){
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+"$$$"+entry.getValue());
}
}
public static void main(String[] args) {
Map<String,String> map = new HashMap<String, String>();
map.put("1", "001");
map.put("2", "002");
map.put("3", "003");
map.put("4", "004");
map.put("5", "005");
//method1(map);
//method2(map);
//method3(map);
method4(map);
}
}
Map遍历的四种方式
最新推荐文章于 2024-07-01 16:13:53 发布