티스토리 뷰
스프링 JPA 공부 중 연관관계에 대해 공부하던 중 외래키가 있는 부분을 연관관계의 주인으로 설정해야된다. 이 때 외래키가 있는 부분은 주로 다에 해당된다.
팀과 멤버가 있을 때 멤버가 팀의 FK를 가지 있다면 멤버가 연관관계의 주인이 되고 일에 해당되는 곳은 자바 컬렉션인 Collection, List, Set, Map 중에 하나를 사용한다.
컬렉션
자바에서 컬렉션이란 데이터의 집합, 그룹을 의미하고 자바컬렉션프레임워크(JCF)는 컬렉션과 이를 정의하는 인터페이스를 제공한다.
List, Set, Queue는 Collection의 하위 인터페이스로서 상속받고 있다. Map도 크게 보면 Collection을 상속받고 있지 않지만 의 일부로 볼 수 있다.
1. Set
Set 인터페이스를 구현하는 클래스로 HashSet, TreeSet이 있다.
Set은 집합 표현으로 순서가 없으며 중복을 허용하지 않는다.
1.1 HashSet
- 가장 빠른 임의 접근 속도
- 순서 예측 X
[코드]
public Set getHashSet() {
Set<Integer> testSet = new HashSet<>();
testSet.add(1);
testSet.add(2);
testSet.add(3);
testSet.add(4);
testSet.add(5);
testSet.add(6);
return testSet;
}
@GetMapping("hashSet")
public Set getHashSet( ) {
return testService.getHashSet();
}
[결과]
[
1,
2,
3,
4,
5,
6
]
1.2 TreeSet
- 정렬 방법 지정 가능
[코드]
public Set getTreeSet() {
Set<Integer> testSet = new TreeSet<>(Comparator.reverseOrder());
testSet.add(1);
testSet.add(2);
testSet.add(3);
testSet.add(4);
testSet.add(5);
testSet.add(6);
return testSet;
}
@GetMapping("treeSet")
public Set getTreeSet( ) {
return testService.getTreeSet();
}
[결과]
[
6,
5,
4,
3,
2,
1
]
2. List
순서가 있는 데이터의 집합으로 중복을 허용하며 List 인터페이스를 구현하는 클래스로 LinkedList, Vector, ArrayList 가 있다.
2.1 LinkedList
- 양방향 포인터 구조로 데이터의 삽입, 삭제에 유리하다.
- 스택, 큐, 양방향 큐를 위한 용도로 쓰인다.
[코드]
public List getLinkedList() {
List<Integer> testList = new LinkedList<>();
testList.add(1);
testList.add(2);
testList.add(3);
testList.add(4);
testList.add(5);
testList.add(6);
return testList;
}
@GetMapping("linkedList")
public List getLinkedList( ) {
return testService.getLinkedList();
}
[결과]
[
1,
2,
3,
4,
5,
6
]
2.2 Vector
- 잘 사용하지 않는다. 대용량 처리에 용이하다.
2.3 ArrayList
- 단방향 포인터 구조로 데이터에 대한 인덱스를 가지고 있어 조회 기능 성능에 뛰어나다.
[결과]
LinkedList와 동일함.
3. Map
Key: Value 쌍으로 이루어진 데이터 집합으로 순서가 없으며, Key의 중복을 허용하지 않는다.
Map 인터페이스는 HashTable, TreeMap, HashMap 클래스에서 구현된다.
3.1 HashTable
- HashMap 보다 느리지만 동기화를 지원하며 Null 값은 허용하지 않는다.
3.2 HashMap
- 중복과 순서가 없으며 Null 값이 허용된다.
[코드]
public Map getHashMap() {
Map<Integer, String> testMap = new HashMap<>();
testMap.put(1, "a");
testMap.put(2, "b");
testMap.put(3, "c");
testMap.put(4, "d");
testMap.put(5, "e");
testMap.put(6, null);
return testMap;
}
@GetMapping("hashMap")
public Map getHashMap( ) {
return testService.getHashMap();
}
[결과]
{
"1": "a",
"2": "b",
"3": "c",
"4": "d",
"5": "e",
"6": null
}
3.3 TreeMap
- 정렬된 순서대로 Key, Value를 저장하여 검색에 용이하다.
[코드]
public Map getTreeMap() {
Map<Integer, String> testMap = new TreeMap<>(Comparator.reverseOrder());
testMap.put(1, "a");
testMap.put(2, "b");
testMap.put(3, "c");
testMap.put(4, "d");
testMap.put(5, "e");
testMap.put(6, null);
return testMap ;
}
@GetMapping("treeMap")
public Map getTreeMap( ) {
return testService.getTreeMap();
}
[결과]
{
"6": null,
"5": "e",
"4": "d",
"3": "c",
"2": "b",
"1": "a"
}
[참고자료]
https://gangnam-americano.tistory.com/41
[JAVA] Java 컬렉션(Collection) 정리
[JAVA] Java 컬렉션(Collection) 정리 ■ Java Collections Framework(JCF) Java에서 컬렉션(Collection)이란 데이터의 집합, 그룹을 의미하며 JCF(Java Collections Framework)는 이러한 데이터, 자료구조인 컬렌션과 이를 구
gangnam-americano.tistory.com
'🍃 스프링' 카테고리의 다른 글
[SPRING] ResponseDTO (0) | 2023.08.28 |
---|---|
[SPRING] GET API Query String (0) | 2023.08.26 |
[SPRING] Entity, DTO (0) | 2023.07.24 |
[SPRING] JPA (0) | 2023.06.05 |
[SPRING] 외부 REST api 사용하기 (0) | 2023.05.20 |