본 영상은 2021-2 KMOOC-데이터엔지니어링 강의의 원본이며, 강좌 오픈 이후에는 질의 응답 채널으로 이용 예정입니다. 각 Topic에 질문 해주시면 가능한 답은 달아드리도록 하겠습니다.
강의노트 (SlideShare) - https://www.slideshare.net/ssuser4a9113
코드 (GitHub) - https://github.com/JaewookByun/data_engineering
총 11강
구성
- ArrayList 연산
- MyArrayList - 배열 기반 List interface 구현
- LinkedList 연산
- MyLinkedList - 참조 기반 List & Queue interfaces 구현
- HashSet/HashMap 연산
- Lambda Expression
- Stream - Open JDK 15에서 이용가능한 거의 대부분의 연산을 다루려고 노력함
1. DataAnalysis
(Lecture 6 – MyLinkedList)
1
Jaewook Byun
Ph.D. Assistant Professor, Department of Software, Sejong University
Associate Director, Auto-ID Labs, Korea
jwbyun@sejong.ac.kr , bjw0829@gmail.com
https://sites.google.com/view/jack-dfpl/home
https://www.youtube.com/channel/UC988e-Y8nto0LXVae0aqaOQ
2. 목차
• 실 세계 데이터 분석을 LinkedList를 통해 수행
• 배열 기반, 참조 기반의 리스트 구현의 장단점을 이해 (삽입(Enqueue))
• List/Queue 인터페이스의 참조기반 자료구조 설계 및 구현
• 실 세계 데이터 분석을 MyLinkedList를 통해 수행
• 삽입(Enqueue) 측면의 배열 기반, 참조 기반의 리스트 분석
2
3. I. LinkedList를 통한 실 세계 데이터 분석
1) List로써의 LinkedList를 이용한 간단한 데이터 분석
3
1. 이벤트의 수 구하기
2. 최소 사람 ID를 구하기
3. 최대 사람 ID를 구하기
유튜브: https://youtu.be/VjOdl0Z5cis
4. I. LinkedList를 통한 실 세계 데이터 분석
2) ArrayList<Email> vs. LinkedList<Email>
4
1. 삽입 (Enqueue)
유튜브: https://youtu.be/3nenst1o8zQ
1
0
9
1
7
2
myArrayList
새로운 instance 마지막에 삽입
start last
9
1
null null
7
offer(5)
1
0 1 2 3
새로운 배열 생성
1
0 1 2 3
인덱스 0 채우기
1
0
9
1 2 3
인덱스 1 채우기 (복사시작)
1
0
9
1
7
2
5
3
새로운 instance 추가
1 2 3 4
1
0
9
1
7
2 3
인덱스 2 채우기 (복사완료)
5
start last
9
1
null null
myLinkedList
7 5
offer(5)
5. I. LinkedList를 통한 실 세계 데이터 분석
2) ArrayList<Email> vs. LinkedList<Email>
5
2. Index 기반 순회
유튜브: https://youtu.be/8cR__Oi0210
myArrayList
1
0
9
1
7
2
5
3
get(2) 1
인덱스 2 직접 접근
1
0
9
1
5
7
3
2
myLinkedList
start last
9
1
null
7 null
5
start last
9
1
null
7 null
5
1 2
get(2)
start 부터 순회 시작
3
6. I. LinkedList를 통한 실 세계 데이터 분석
2) ArrayList<Email> vs. LinkedList<Email>
6
3. 추출 (Dequeue)
https://youtu.be/6AQx4kC424E
poll()
poll() 1
0 1 2
새로운 배열 생성
9
0 1 2
0 이후 요소 복사 시작
1 2 3 4
myLinkedList
start last
9
1
null
7 null
5
myArrayList
1
0
9
1
7
2
5
3
9
0
7
1 2
9
0
7
1
5
2
나머지 요소 복사 완료
start last
9
1
null
7 null
5
null
참조의 재조정으로 효율적으로 해결
7. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
1) LinkedList 복습
7
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
int size() Collection의 크기를 반환
boolean add(E e) Collection에 새로운 instance를 삽입 (Capacity 초과시 Exception 발생)
void add(int index, E element) List의 특정 위치에 instance를 삽입
boolean offer(E e) Queue에 새로운 instance를 마지막에 삽입 (Capacity 초과시 false 반환)
boolean contains(Object o) Collection에 o라는 instance가 있는지 확인
E get(int index) List의 특정 위치에 있는 instance를 추출
int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터)
int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터)
E element() Queue의 처음 instance를 반환 (비어있을 때 Exception 발생)
E peek() Queue의 처음 instance를 반환 (비어있을 때 null 반환)
E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트
boolean remove(Object o) Collection에 o라는 instance가 있다면 삭제
E remove(int index) List의 특정 위치에 있는 instance를 삭제
E remove() Queue의 처음 instance를 지우고 반환 (비어있을 때 Exception 발생)
E poll() Queue의 처음 instance를 지우고 반환 (비어있을 때 null 반환)
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
void clear() Collection을 비움
Object[] toArray() Collection을 배열에 담음
void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함
List<E> subList(int from, int to) List의 부분을 가져온다
META
C
R
D
T
U
8. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
2) MyLinkedList 설계 전략
8
MyNode
prev
MyNode
next
E
item
MyNode:
3
MyLinkedList:
first last
2
1
null null
size = 3
index = 0
get(0) → 1
Poll
Offer
9. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
3) MyLinkedList 구현 준비
9
• MyNode<E> 생성
• MyNode<E> prev;
• E item;
• MyNode <E> next;
• List<E> 와 Queue<E> 인터페이스를 구현한 MyLinkedList<E> 생성
• MyNode<E> first;
• MyNode<E> last;
• int size;
• 유튜브: https://youtu.be/I5fZVmGzJiE
10. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
10
• size가 0이면 true
• 유튜브: https://youtu.be/aeFgEHp6G9k
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
3
first last
2
1
null null
size = 3
return size == 0;
11. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
11
• size를 반환
• 유튜브: https://youtu.be/873l54fbLIE
Return Type Method Description
int size() Collection의 크기를 반환
3
first last
2
1
null null
size = 3
return size;
12. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
12
Return Type Method Description
boolean add(E e) Collection에 새로운 instance를 삽입
• MyLinkedList가 비어 있지 않을 때
• 유튜브: https://youtu.be/I40FfKGdelQ
3
first
2
1
null null
size = 3
add(3)
(1) MyNode newNode 생성
prev = last;
item = e;
next = null;
(2) last.next 참조 갱신
last.next = newNode;
last
(3) last 갱신
(4) size++
13. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
13
• size가 0이면 []를 반환
• size가 0이 아니라면, first 부터 시작하여 getNext()가 null일 때까지 순회하며 문자열을
생성하여 반환
• 유튜브: https://youtu.be/dwtKUYFV-CA
Return Type Method Description
String toString() MyLinkedList의 문자열 표현을 반환한다.
14. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
14
• for-each loop를 이용하여 구현
• 유튜브: https://youtu.be/pa9MuYx17cQ
Return Type Method Description
생성자 MyLinkedList() 빈 LinkedList를 생성
생성자 MyLinkedList(Collection c) LinkedList를 Collection c의 요소를 가져와 생성
JVM Heap
MyLinkedList<Integer> linkedList = new MyLinkedList<Integer>();
MyLinkedList
[ ]
1. 빈 리스트 생성
2. 주소할당
Collection<Integer> collection = arrayList; // [0,1,2,3,4] ArrayList
MyLinkedList<Integer> linkedList = new MyLinkedList<Integer>(collection);
MyLinkedList
[0,1,2,3,4]
1. 컬렉션으로 리스트 생성
2. 주소할당
15. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
15
Return Type Method Description
boolean contains(Object o) Collection에 o라는 instance가 있는지 확인
E get(int index) List의 특정 위치에 있는 instance를 추출
int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터)
int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터)
3
first last
2
1
null null
2
contains(o)
get(index)
indexOf(o)
(1) item이 o인지 확인하거나
index가 맞는지 확인
(2) 맞다면,
true, item, index 반환
(3) 틀리다면, next로 계속 이동하여 (1) (2) 수행
(4) next가 null에 도달시,
false, Exception, -1 반환
사전에 throw됨
유튜브: https://youtu.be/ugeWL5DD1QE
16. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
16
유튜브: https://youtu.be/Z_6Kv2ussZ0
Return Type Method Description
void add(int index, E element) List의 특정 위치에 instance를 삽입
• MyLinkedList의 마지막에 추가할 경우
• add(E e)와 같음
• MyLinkedList의 중간에 추가할 경우
관찰:
• first 와 last는 다르다
• next를 get(index)를 갖고 있는 노드로 지정
• prev를 get(index)를 갖고 있는 노드의 prev로 지정
• 새로운 노드는 next, prev 사이에 위치
• first와 prev는 같을 수 있음
• next와 last는 같을 수 있음
• first가 null일 때, 새로 추가될 노드는 first가 됨
17. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
17
유튜브: https://youtu.be/IVx3iUlbd4A
Return Type Method Description
E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트
boolean remove(Object o) Collection에 o라는 instance가 있다면 삭제
E remove(int index) List의 특정 위치에 있는 instance를 삭제
관찰:
• 만약 prev이 null이라면
• 지워질 노드는 first였었음
• 새로운 first는 next가 됨
• 만약 prev가 null이 아니라면
• prev.setNext(next)로 prev와 next를 연결해준다.
• 만약 next이 null이라면
• 지워질 노드는 last였었음
• 새로운 last는 prev가 됨
• 만약 next가 null이 아니라면
• next.setPrev(prev)로 prev와 next를 연결해준다.
18. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
18
유튜브: https://youtu.be/9r-u4TSadEI
Return Type Method Description
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
3
start last
2
1
null null
index: -1 index: 0 index: 1 index: 2
(size-1)
index: size
List list에 대한 Cursor와 메소드 동작
초기 cursor 값
iterator() → -1
listIterator(int index) → index -1
즉, iterator() == listIterator(0)
previousIndex() cursor 반환
previous() list.get(cursor) 반환 후 cursor-- 수행
hasPrevious() list.get(cursor) == null 반환
nextIndex() cursor + 1 반환
next() list.get(cursor+1) 반환 후 cursor++ 수행
hasNext() list.get(cursor+1) == null 반환
ListIterator<Integer> ai = list.listIterator(0);
while (ai.hasNext()) {
Integer value = ai.next(); // Do Something
}
ListIterator<Integer> di =
list.listIterator(list.size());
while (di.hasPrevious()) {
Integer value = di.previous();// Do Something
}
순회 패턴1: 앞에서 부터
순회 패턴2: 뒤에서 부터
19. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
19
유튜브: https://youtu.be/Bbz8UYsbE7w
• toArray(): size만큼의 Object[] 생성 후, iterator를 이용하여 추가
• sort: toArray와 listIterator의 구현으로 동작을 보장
Return Type Method Description
Object[] toArray() Collection을 배열에 담음
void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함
인덱스
값
Object[]
toArray()
sort(c)
subList(2,4)
first last
20. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
20
유튜브: https://youtu.be/fSWMkYQJAR4
• add, offer: capacity가 없으므로 구현이 동일
• clear(): remove(0)를 계속 호출하여 구현
Return Type Method Description
boolean add(E e) Collection에 새로운 instance를 삽입 (Capacity 초과시 Exception 발생)
boolean offer(E e) Queue에 새로운 instance를 마지막에 삽입 (Capacity 초과시 false 반환)
void clear() Collection을 비움
first last
2
1 3
4
last
first
2
1 3
add(4)
offer(4)
first last
null
21. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
21
유튜브: https://youtu.be/giUWhdZFJd4
Return Type Method Description
E element() Queue의 처음 instance를 반환 (비어있을 때 Exception 발생)
E peek() Queue의 처음 instance를 반환 (비어있을 때 null 반환)
first last
2
1 3
element()
peek()
1 반환
first last
null
element()
peek() null 반환
22. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
22
유튜브: https://youtu.be/keSPEaWFxws
Return Type Method Description
E remove() Queue의 처음 instance를 지우고 반환 (비어있을 때 Exception 발생)
E poll() Queue의 처음 instance를 지우고 반환 (비어있을 때 null 반환)
start last
2
1 3
start
2
1 3
remove()
poll()
last
23. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
23
유튜브: https://youtu.be/zi2HewCHpiA
1. 이벤트의 수 구하기
2. 최소 사람 ID를 구하기
3. 최대 사람 ID를 구하기
24. II. List, Queue 인터페이스의 참조기반 자료구조 설계 및 구현
4) MyLinkedList 구현
24
유튜브: https://youtu.be/vqFleHXOOso
1. 삽입 (Enqueue)
1
0
9
1
7
2
myArrayList
새로운 instance 마지막에 삽입
start last
9
1
null null
7
offer(5)
1
0 1 2 3
새로운 배열 생성
1
0 1 2 3
인덱스 0 채우기
1
0
9
1 2 3
인덱스 1 채우기 (복사시작)
1
0
9
1
7
2
5
3
새로운 instance 추가
1 2 3 4
1
0
9
1
7
2 3
인덱스 2 채우기 (복사완료)
5
start last
9
1
null null
myLinkedList
7 5
offer(5)