728x90
HashMap์ Map์ธํฐํ์ด์ค์ ํ์ข ๋ฅ๋ก์จ Key์ Value ๊ฐ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ํํ๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
TreeMap์ ์ด์งํธ๋ฆฌ ๊ตฌ์กฐ๋ค.
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package main;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
public class MainClass {
public static void main(String[] args) {
/*
Hashmap : ์ฌ์
("์ฌ๊ณผ" : "apple")
key : value - > json
TreeMap : HashMap + sorting
*/
HashMap<Integer,String> hMap = new HashMap<Integer,String>();
//2 -> Map<Integer,String> hMap = new HaspMAp<Integer,String>();
//์ถ๊ฐ key value + ์ํ๋ ์์น์ ์ถ๊ฐ๋ ใ
๋ค.
hMap.put(11,"์ฝ์
");
hMap.put(22, "์ด์ญ์ด");
hMap.put(33, "์ผ์ญ์ฝ");
String value = hMap.get(22);// ์ค๊ฐ ๋ฐธ๋ฅ ํค๊ฐ์ผ๋ก ๊บผ๋ด๊ธฐ
System.out.println(value);//์ด์ญ์ด
//์ญ์
String del = hMap.remove(11);
System.out.println("์ญ์ ๋ ๋ฐ์ดํฐ=" + del); //์ญ์ ๋ ๋ฐ์ดํฐ๋ฅผ ์ฝ์
์ถ๋ ฅ
System.out.println("map์ ํฌ๊ธฐ=" + hMap.size());
//๊ฒ์
//์๋ค/์๋ค
boolean b = hMap.containsKey(22);
if(b == true) {
String s = hMap.get(22);
System.out.println("value = " + s);
}
//์์
hMap.replace(22, "์ด์ญ + ์ด");
System.out.println(hMap.get(22));
hMap.put(22, "์ด์ญํ๋ฌ์ค์ด");//์ค๋ณต ๋ฏธํ์ฉ
System.out.println(hMap.get(22));
//๋ชจ๋ ํค ๊ฐ์ ์ทจ๋ํ๋ค
Iterator<Integer> it = hMap.keySet().iterator();
while(it.hasNext()) {
Integer k = it.next();
System.out.println("key" + k);
}
//์ข์ํ๋ ๊ณผ์ผ
//key:value -> String
HashMap<String, String> fruit = new HashMap<String, String>();
fruit.put("apple", "์ฌ๊ณผ");
fruit.put("strawberry", "๋ธ๊ธฐ");
fruit.put("melon", "๋ฉ๋ก ");
fruit.put("lemon", "๋ ๋ชฌ");
fruit.put("pear", "๋ณต์ญ์");
//๋ชจ๋ ํค๊ฐ์ ์ทจ๋
Iterator<String> it1 = fruit.keySet().iterator();
while(it1.hasNext()) {
String k = it1.next();
String v = fruit.get(k);
System.out.println("key (๊ณผ์ผ์์ด๋ก)" + k + v);
}
//TreeMap
//sorting
TreeMap<String,String> tMap = new TreeMap<String,String>(fruit);
//์ค๋ฆ
//Iterator<String> itkey = tMap.KeySet().iterator();
//์๋ ์ค๋ฆ ์ฐจ์
//TreeMap์ Sorting์ด ํฌํจ๋จ
//๋ด๋ฆผ
Iterator<String> itkey = tMap.descendingKeySet().iterator();
System.out.println("=====================");
while(itkey.hasNext()) {
String k = itkey.next();
String v = tMap.get(k);
System.out.println("key" + k + "value " + v);
}
}
}
|
cs |
'๐ ์๋ฐ Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Singleton(์ฑ๊ธํค ํจํด) (0) | 2021.02.04 |
---|---|
List์ HashMap, Iterator์ ํ์ฉํ ์ผ๊ตฌ ์ ์ ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ (0) | 2021.02.03 |
LinkedList ๊ฐ๋จ ์ ๋ฆฌ & ์์ฝ (0) | 2021.02.03 |
์์๊ณผ ๋ถ๋ชจํด๋์ค ์์ํด๋์ค (0) | 2021.02.01 |
์๋ฐ(JAVA)- ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋จ์ด๋? (0) | 2021.02.01 |
๋๊ธ