import java.util.Map;
public class MapTest{
public static void main(String[] args){
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
map.put("key5", "value5");
// solution 1
Iterator keys = map.keySet().iterator();
while(keys.hasNext()){
String key = keys.next();
System.out.println("key: " + key + ", value: " + map.get(key));
}
// solution 2
for( Map.Entry elem : map.entrySet() ){
System.out.println( String.format("key : %s, value : %s", elem.getKey(), elem.getValue()) );
}
// solution 3
for( String key : map.keySet() ){
System.out.println( String.format("key : %s, value : %s", key, map.get(key)) );
}
}
}
2014년 8월 19일 화요일
Java Map을 iteration 시키는 세 가지 방법.
Three way to iterate Java Map.
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기