<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번만/오름차순
}
}
어느 collections을 써야 할까 ?
>
Map 비교 간단코딩 참고
http://rangken.github.io/blog/2015/java.map/
Set 비교 간단 코딩 참고
'JavaCode(review)' 카테고리의 다른 글
collections challenge 종합 (0) | 2020.07.17 |
---|---|
Map.entry -collections / unmodifiable Map (0) | 2020.07.16 |
InstanceOF (0) | 2020.07.14 |
Set - Symmetric&Asymmetric ( Set Interface Bulk Operation) - collection (0) | 2020.07.13 |
HashSet Interface- collection (0) | 2020.07.09 |