Java/Script

HashMap 과 TreeMap

CleanSense 2018. 2. 19. 19:45
728x90

코드설명 : HashMap 과 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
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
 
public class MainClass {
 
    public static void main(String[] args) {
 
        
        hashMap : key값과 value값 한쌍으로 관리 된다.
                                  
        TreeMap : hashMap + sorting
        
        
        HashMap<Integer, String> hmap = new HashMap<Integer, String>(); 
        
        .put -> HashMap의 자료넣기
        hmap.put(11"십일");
        hmap.put(12"십이");
        hmap.put(13"십삼");
        
        .get(key) -> key값으로 value값을 가져온다.
        String value = hmap.get(12); .
        System.out.println("value : "+value);    // value : 십이
        
        .containsKey(Object key) -> key와 연동되는 value값의 존재파악 
        boolean b = hmap.containsKey(new Integer(15));
        System.out.println(b);    // false        
        
        .remove(key) -> 해당key에 있는 value값을 삭제한다. 리턴값은 String        
        String s = hmap.remove(13);         
        
        .replace(key, value) -> 해당 key에 있는 value값을 수정한다.
        hmap.replace(12"10+2");    
        
        
        
        // 데이터 전체 출력 Iterator사용
 
        Iterator<Integer> keys = hmap.keySet().iterator();
        while (keys.hasNext()) {
            Integer key = keys.next();
            System.out.println("key : " + key + " value : " + hmap.get(key));
        }
        System.out.println();
 
        // for each
        for (Integer i : hmap.keySet()) {
            System.out.println("key : " + i + " value : " + hmap.get(i));
        }
        System.out.println();
 
        // HashMap<Integer , Human>
 
        HashMap<StringString> smap = new HashMap<StringString>();
 
        .put(key, value)
        smap.put("aaaa""사과");
        smap.put("dddd""배");
        smap.put("cccc""포도");
 
        .get(key)
        String val = smap.get("apple");
        System.out.println("val : " + val);
 
        .replace(key, value)
        smap.replace("apple""iPhone");
        value = smap.get("apple");
        System.out.println("value : " + value);
 
        
        TreeMap<StringString> treemap = new TreeMap<StringString>(smap);
 
        
        TreeMap.keySet().iterator() -> key값을 기준으로 내림차순 정렬
        Iterator<String> itkey = treemap.keySet().iterator();         
         while(itkey.hasNext()) { 
        String key = itkey.next();
         System.out.println("key : "+key + ", value : "+treemap.get(key));
        }
         
        
        TreeMap.descendingKeySet().iterator() -> key값을 기준으로 오름차순 정렬
        Iterator<String> itkey = treemap.descendingKeySet().iterator();
 
        while (itkey.hasNext()) {
            String key = itkey.next();
            System.out.println("key : " + key + ", value : " + treemap.get(key));
        }
    }
}
 
cs


728x90