자바 HashMap 예제
Ex)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
37
38
39
40
41import java.util.*;
public class HashMapTest {
public static void main(String[] args) {
boolean rtn = false;
int hashMapSize = 0;
String value1 = "01012341234";
HashMap<String, String> hm = new HashMap<String, String>();
hm.put(value1, "billing_account");
hm.put("01612341234", "billing_account");
hm.put("01099992580", "customer");
// hashmap size check : 3
hashMapSize = hm.size();
System.out.println("Hashmap size = [" + hashMapSize + "]");
// hashmap print
Set set = hm.keySet();
Object[] hmKeys = set.toArray();
for (int i = 0; i < hashMapSize; i++) {
String key = (String) hmKeys[i];
System.out.println("key[" + key + "], value["
+ (String) hm.get(key) + "]");
}
// exist check : true
rtn = hm.containsKey(value1);
if (rtn == true) {
System.out.println("key 01012341234 exist");
System.out.println("value=[" + (String) hm.get(value1) + "]");
} else {
System.out.println("key 01012341234 not exist");
}
// key remove
hm.remove(value1);
// hashmap size check : 3 ->2
hashMapSize = hm.size();
System.out.println("Hashmap size = [" + hashMapSize + "]");
// all clear
hm.clear();
// hashmap size check : 2 ->0
hashMapSize = hm.size();
System.out.println("Hashmap size = [" + hashMapSize + "]");
}
}
Ex ) 버전 차이 설명
1 |
|