java|Java中Map的简单用法

1.声明一个Map :
Map map = new HashMap();
2 .向map中放值 ,注意: map是key-value的形式存放的,如:
map.put(“sa”,“dd”);
3 .从map中取值 :
String str = map.get(“sa”).toString,
结果是: str = "dd’
4 .遍历一个map,从中取得key和value :
遍历key:

Map map = new HashMap(); for (Integer key : map.keySet()) { System.out.println("Key = " + key); }

遍历value:
for (Integer value : map.values()) { System.out.println("Value = "https://www.it610.com/article/+ value); }

【java|Java中Map的简单用法】遍历key和value:
Map map = new HashMap(); for (Map.Entry entry : map.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = "https://www.it610.com/article/+ entry.getValue()); }

    推荐阅读