[java入門]マップ(Map)の中身を全て取得する方法まとめ
javaコード
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Weees {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put(“1", “value1");
map.put(“2", “value2");
map.put(“3", “value3");
//方法1
System.out.println(“Map.keySet method11:");
for (String key : map.keySet()) {
System.out.println(“key= “+ key + " and value= " + map.get(key));
}
//方法2
System.out.println(“Map.entrySet iterator method22");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println(“key= " + entry.getKey() + " and value= " + entry.getValue());
}
//方法3
System.out.println(“Map.entrySet method33");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(“key= " + entry.getKey() + " and value= " + entry.getValue());
}
//方法4
System.out.println(“Map.values() method44");
for (String v : map.values()) {
System.out.println(“value= " + v);
}
}
}