ArrayList 와 비슷하다.
Key와 Value로 나뉘고 값을 가져오기 위해선 key를 알아야한다.
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
|
import java.util.HashMap;
import java.util.Iterator;
public class MyClass{
public static void main(String args[]){
// 키와 값
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(0, "str0");
hashMap.put(3, "str3");
hashMap.put(5, "str5");
hashMap.put(10, "str10");
System.out.println(hashMap.toString());
System.out.println(hashMap.get(3)); // str 3
hashMap.remove(5);
System.out.println(hashMap.toString());
hashMap.clear();
System.out.println(hashMap.toString());
hashMap.put(0, "str0");
hashMap.put(3, "str3");
hashMap.put(5, "str5");
hashMap.put(10, "str10");
System.out.println(hashMap.toString());
Iterator<Integer> iterator = hashMap.keySet().iterator();
// 반복문
while(iterator.hasNext()){
System.out.println(hashMap.get(iterator.next()));
}
}
}
| cs |
result
댓글
댓글 쓰기