赞
踩
1、遍历Key
System.out.println("遍历 key:");
for(String key : map.keySet()){
System.out.println(key + ":" + map.get(key));
}
2、遍历value
System.out.println("遍历 value:");
for(Object value : map.values()){
System.out.println(value.toString());
}
3、遍历entry
System.out.println("遍历 entry:");
for(Map.Entry<String, Object> obj : map.entrySet()){
System.out.println(obj.getKey() + ":" + obj.getValue());
}
4、迭代器
System.out.println("迭代器:");
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()){
String key = iterator.next();
System.out.println(key + ":" + map.get(key));
}
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
Iterator<Map.Entry<String, Object>> iterator1 = entrySet.iterator();
while (iterator1.hasNext()){
Map.Entry<String, Object> entryObj = iterator1.next();
System.out.println(entryObj.getKey() + ":" + entryObj.getValue());
}
测试
Map<String, Object> map = new HashMap<>();
map.put("1", 1);
map.put("2", 2);
map.put("3", 3);
map.put("4", 4);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。