Programming/Java
Java Map 반복(Iteration) 시키는 방법
Yizi
2018. 7. 24. 11:54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 |
package com.tistory.jononeworld;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapIterationSample {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("키1", "값1");
map.put("키2", "값2");
map.put("키3", "값3");
map.put("키4", "값4");
map.put("키5", "값5");
map.put("키6", "값6");
//방법1
Iterator<String> keys = map.keySet().iterator();
while( keys.hasNext() ){
String key = keys.next();
System.out.println( String.format("키: %s, 값: %s", key, map.get(key)) );
}
//방법2
for( Map.Entry<String, Object> elem : map.entrySet() ){
System.out.println( String.format("키: %s, 값: %s", elem.getKey(), elem.getValue()) );
}
//방법3
for( String key: mep.keySet() ){
System.out.println( String.format("키: %s, 값: %s", key, map.get(key)) );
|