随便往map里面加点儿数
public static Map<String, Object> addMap() {
Map<String, Object> map = new HashMap<String, Object>();
for(int i=1; i<=15; i++) {
map.put("第" + i + "个数", i);
}
return map;
}
方式一:ForEach遍历map.entrySet
for(Entry<String, Object> entry : map.entrySet()) {
System.out.print("key=" + entry.getKey() + ",value=" + entry.getValue() + "; ");
}
方式二:ForEach遍历map的key
for(String key : map.keySet()) {
System.out.print("key=" + key + ",value=" + map.get(key) + "; ");
}
方式三:ForEach遍历map的value
for(Object value : map.values()) {
System.out.print("value=" + value + "; ");
}
方式四:迭代遍历map的EntrySet
Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
System.out.print("key=" + entry.getKey() + ",value=" + entry.getValue() + "; ");
}