본문 바로가기

JavaCode(review)

Map, Set - collections 개념 간단 비교정리

반응형

<Map>

HashMap : 순서보장 x -> 일반적으로 사용 : 검색성능이 좋음

LinkedHashMap : 입력순

TreeMap : 알파벳순 - comparator 구현

 

<Set>

HashSet : 순서보장 x

LinkedHashSet : 입력순

TreeSet : 오름차순 데이터 정렬

 

>sorted collections : TreeMap, TreeSet

>

Map : (key, value) : 검색 용이. (for-Map.Entry 이용하거나 for문만 이용하거나 - collections challenge 종합에 있음.)/https://gb-codingworld.tistory.com/68

Set : (value), 중복불가- count1번만, 순서가x-집합개념 , 데이터검색시 iterator사용

List : 순서가 있음.

 

package my.gabin.com;

import java.util.*;

public class Main {


    private static Map<String,Integer> hashM = new HashMap<String,Integer>();
    private static Map<String,Integer> treeM = new TreeMap<String,Integer>();
    private static Set<Integer> hashS = new HashSet<>();
    private static Set<Integer> treeS = new TreeSet<>();


    public static void main(String[] args) {
	// write your code here
        hashM.put("a",1);
        hashM.put("b",2);
        hashM.put("b",3); // b->3으로 출력
        hashM.put("c",2);
        hashM.put("e",2);

        System.out.println("hashMap = " + hashM);   // hashMap = {a=1, b=3, c=2, e=2}

        treeM.put("a",3);
        treeM.put("b",2);
        treeM.put("f",1);
        treeM.put("c",2);
        treeM.put("e",2);

        System.out.println("treeMap = "+ treeM); // treeMap = {a=3, b=2, c=2, e=2, f=1} 알파벳순
        
        hashS.add(1);
        hashS.add(2);
        hashS.add(4);
        hashS.add(2);
        hashS.add(3);

        System.out.println("hashSet = " +hashS); // hashSet = [1, 2, 3, 4] /count1번만

        treeS.add(1);
        treeS.add(2);
        treeS.add(4);
        treeS.add(2);
        treeS.add(3);

        System.out.println("treeSet = "+treeS); // treeSet = [1, 2, 3, 4] /count1번만/오름차순

    }
}

 

출처  https://12bme.tistory.com/91
출처  https://m.blog.naver.com/PostView.nhn?blogId=javaking75&logNo=140188296229&proxyReferer=https:%2F%2Fwww.google.com%2F

 

 

어느 collections을 써야 할까 ?

출처. https://codeasign.com/2020/01/28/java-collections-how-to-choose-collection/

 

   

>

Map 비교 간단코딩 참고

http://rangken.github.io/blog/2015/java.map/

 

Set 비교 간단 코딩 참고

 

http://blog.naver.com/PostView.nhn?blogId=heartflow89&logNo=220994601249&parentCategoryNo=&categoryNo=28&viewDate=&isShowPopularPosts=false&from=postView