Javaにおけるmapの3つの遍歴方式
1305 ワード
public class Test {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put(1,"a");
map.put(2,"b");
map.put(3,"c");
// : key
System.out.println(" : key");
Set keySet = map.keySet();
for (Integer key:keySet) {
System.out.println(key+" "+map.get(key));
}
// : value
System.out.println(" : value");
Set> entrySet = map.entrySet();
for (Map.Entry entry:entrySet){
System.out.println(entry.getKey()+" "+entry.getValue());
}
// :Iterator( )
System.out.println(" :Iterator( )");
Iterator> it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = it.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
// values
map.values();
for (String value:map.values()) {
System.out.println(value);
}
}
}