티스토리 뷰

반응형


최근 몇몇 기업의 코딩테스트를 보면서

자주 사용되었던 Map, Set 자료형에 대해서 정리를 해보려고 한다 !


HashMap 은 실무에서도 자주 사용된다고 하니 익숙하게 적응해두면 좋을 것 같다.

자주 사용되는 Method 위주로 정리해보았다.


. HashMap


- Map은 Key와 Value라는 것을 한 쌍으로 갖는 자료형

- 리스트나 배열처럼 순차적으로 해당 요소 값을 구하지 않고 key를 통해 value를 얻는다

- Map의 가장 큰 특징이라면 key로 value를 얻어낸다는 점




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
import java.util.HashMap;
import java.util.Iterator;
 
public class mapTest {
 
    public static void main(String[] args) {
 
        // HashMap 선언
        HashMap<Integer, String> mapTest = new HashMap<>();
        
        // 데이터 삽입
        mapTest.put(1"Java");
        mapTest.put(2"C++");
        mapTest.put(3"python");
        mapTest.put(4"JavaScript");
        
        // Key 삭제
        mapTest.remove(4);
 
        // Key 존재 확인
        System.out.println(mapTest.containsKey(4)); // true
        
        // Value 존재 확인
        System.out.println(mapTest.containsValue("Java")); // true
        
        // Key의 Value 확인
        System.out.println(mapTest.get(1)); // Java
        
        // isEmpty()
        System.out.println(mapTest.isEmpty()); // false
        
        // check HashMap size
        System.out.println(mapTest.size()); // 3
        
        // HashMap toString
        System.out.println(mapTest.toString()); // {1=Java, 2=C++, 3=python}
        
        // Return a Collection view of the values contained in this map
        System.out.println(mapTest.values()); // [Java, C++, python]
 
        // 데이터 출력
        // keySet : a set view of the keys contained in this map
        Iterator<Integer> keys = mapTest.keySet().iterator();
        while (keys.hasNext()) {
            int key = keys.next();
            String value = mapTest.get(key);
 
            System.out.println(key + " " + value);
        }
 
        // HashMap removes all of the mappings from this map
        mapTest.clear();
 
    }
 
}
 
cs






. HashSet


- 중복을 허용하지 않고 순서가 없는 자료형



- 순서가 없는 자료형이라서 출력 시 뒤죽박죽으로 출력되는 것을 확인할 수 있다.


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
import java.util.HashSet;
import java.util.Iterator;
 
public class setTest {
    
    public static void main(String[] args) {
        
        // HashSet 선언
        HashSet<Integer> setTest = new HashSet<>();
        
        // 데이터 삽입
        setTest.add(10);
        setTest.add(20);
        setTest.add(30);
        setTest.add(40);
        setTest.add(50);
        
        // 중복을 허용하지 않음
        setTest.add(10);
        setTest.add(20);
        setTest.add(30);
        
        // remove Element
        setTest.remove(40);
        
        // Element 존재 확인
        System.out.println(setTest.contains(10)); // true
 
        // isEmpty()
        System.out.println(setTest.isEmpty()); // false
        
        // size()
        System.out.println(setTest.size()); // 4
        
        // get iterator()
        Iterator setIter = setTest.iterator();
        while (setIter.hasNext()) {
            System.out.println(setIter.next());
        }
        
        // Removes all of the elements from this set.
        setTest.clear();
        
    }
}
 
cs



반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday