본 영상은 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에서 이용가능한 거의 대부분의 연산을 다루려고 노력함
3. I. java.util.LinkedList 소개
1) java.util.LinkedList의 계층구조
3
• 본 과정에서는 List + Queue 로서의 LinkedList를 살펴볼 예정
Collection
List
ArrayList
LinkedList
Queue
4. I. java.util.LinkedList 소개
2) List 복습
4
• 순서화 된 중복을 허용하는 Collection
• Collection 내의 Instance가 위치할 위치인 Index를 통해 조작
• 구현에 따라 제공하는 연산의 효율이 다를 수 있음
• 구현물: ArrayList, LinkedList, Stack 등
Collection
List
ArrayList
중복허용 순서 유지 Thread-Safe
O O ?
중복허용 순서 유지 Thread-Safe
O O X
LinkedList
중복허용 순서 유지 Thread-Safe
O O X
LinkedList
중복허용 순서 유지 Thread-Safe
O O O
5. I. java.util.LinkedList 소개
2) List 복습
5
• 순서화 된 중복을 허용하는 Collection
• Collection 내의 Instance가 위치할 위치인 Index를 통해 조작
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
int size() Collection의 크기를 반환
boolean add(E e) Collection에 새로운 instance를 삽입
void add(int index, E element) List의 특정 위치에 instance를 삽입
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 set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트
boolean remove(Object o) Collection에 o라는 instance가 있다면 삭제
E remove(int index) List의 특정 위치에 있는 instance를 삭제
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의 부분을 가져온다
Stream<E> stream() Collection에 대한 Stream을 반환
META
C
R
D
T
U
6. I. java.util.LinkedList 소개
3) Queue 소개
6
• 삽입과 추출이 한 방향에서만 일어나는 컬렉션
• 먼저 삽입된 instance가 먼저 추출됨
• First In First Out (FIFO)
• 응용: 버퍼, 스케쥴러
Last
First
Offer
Poll
7. I. java.util.LinkedList 소개
3) Queue 소개
7
• 삽입과 추출이 한 방향에서만 일어나는 컬렉션
• 먼저 삽입된 instance가 먼저 추출됨
• First In First Out (FIFO)
• 응용: 버퍼, 스케쥴러
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
int size() Collection의 크기를 반환
boolean add(E e) Collection에 새로운 instance를 삽입 (Capacity 초과시 Exception 발생)
boolean offer(E e) Queue에 새로운 instance를 마지막에 삽입 (Capacity 초과시 false 반환)
boolean contains(Object o) Collection에 o라는 instance가 있는지 확인
E element() Queue의 처음 instance를 반환 (비어있을 때 Exception 발생)
E peek() Queue의 처음 instance를 반환 (비어있을 때 null 반환)
boolean remove(Object o) Collection에 o라는 instance가 있다면 삭제
E remove() Queue의 처음 instance를 지우고 반환 (비어있을 때 Exception 발생)
E poll() Queue의 처음 instance를 지우고 반환 (비어있을 때 null 반환)
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
void clear() Collection을 비움
Object[] toArray() Collection을 배열에 담음
Stream<E> stream() Collection에 대한 Stream을 반환
META
C
R
D
T
8. I. java.util.LinkedList 소개
4) LinkedList 소개
8
• Array를 기반으로 한 것이 아닌, 객체 Node 간을 연결을 이용하여 설계한 컬렉션
• java.util.LinkedList는 객체가 다음의 객체와 이전의 객체에 대한 참조를 갖고 있는
Doubly Linked List 임
유튜브 - https://youtu.be/rwt1xIB7UBw
prev prev
data
Node:
3
Doubly
Linked List: start last
2
1
null null
size = 3
index = 0
get(0) → 1
Poll
Offer
9. II. java.util.LinkedList CRUD
1) LinkedList 복습
9
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
10. II. java.util.LinkedList CRUD
2) LinkedList CRUD
10
• Array를 기반으로 한 것이 아닌, 객체 Node 간을 연결을 이용하여 설계한 Doubly
Linked List 구현
유튜브 - https://youtu.be/f_qQbQHfAO0
Return Type Method Description
생성자 LinkedList() 빈 LinkedList를 생성
생성자 LinkedList(Collection c) LinkedList를 Collection c의 요소를 가져와 생성
JVM Heap
LinkedList<Integer> linkedList = new LinkedList<Integer>();
LinkedList
[ ]
1. 빈 리스트 생성
2. 주소할당
Collection<Integer> collection = arrayList; // [0,1,2,3,4] ArrayList
LinkedList<Integer> linkedList = new LinkedList<Integer>(collection);
LinkedList
[0,1,2,3,4]
1. 컬렉션으로 리스트 생성
2. 주소할당
11. II. java.util.LinkedList CRUD
2) LinkedList CRUD
11
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를 삽입
3
start last
2
1
null null
isEmpty() → false
size() → 3
add(4)
3
start last
2
1
null 4
생성 및 참조 조정
유튜브 - https://youtu.be/zbs4RhZ13sw
12. II. java.util.LinkedList CRUD
2) LinkedList CRUD
12
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
contains(1)
true
2
get(3)
3
indexOf(2) lastIndexOf(2)
1 2
유튜브 - https://youtu.be/0mMDuAeqZOo
13. II. java.util.LinkedList CRUD
2) LinkedList CRUD
13
Return Type Method Description
E set(int index, E element) 컬렉션의 특정 위치의 instance 값을 element로 업데이트
boolean remove(Object o) 컬렉션에 o라는 instance가 있다면 삭제
E remove(int index) 컬렉션의 특정 위치에 있는 instance를 삭제
3
start last
2
1
3
null null
2
set(0,3)
remove(3)
3
start last
2
3
null null
2 null
제거 및 참조 조정
유튜브 - https://youtu.be/qqdkEkk20gA
14. II. java.util.LinkedList CRUD
2) LinkedList CRUD
14
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: 뒤에서 부터
유튜브 - https://youtu.be/AFuXpnPsHfs
15. II. java.util.LinkedList CRUD
2) LinkedList CRUD
15
유튜브 - https://youtu.be/iZHVYhwmyR8
Return Type Method Description
void clear() Collection을 비움
Object[] toArray() Collection을 배열에 담음
void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함
List<E> subList(int from, int to) List의 부분을 가져온다
first last
null
clear()
인덱스
값
Object[]
toArray()
sort(c)
subList(2,4)
first last
16. II. java.util.LinkedList CRUD
2) LinkedList CRUD
16
유튜브 - https://youtu.be/8Vp477YQNE8
Return Type Method Description
boolean add(E e) Collection에 새로운 instance를 삽입 (Capacity 초과시 Exception 발생)
boolean offer(E e) Queue에 새로운 instance를 마지막에 삽입 (Capacity 초과시 false 반환)
first last
2
1 3
4
last
first
2
1 3
add(4)
offer(4)
17. II. java.util.LinkedList CRUD
2) LinkedList CRUD
17
유튜브 - https://youtu.be/U2ndWwtSxWQ
Return Type Method Description
E element() 컬렉션의 처음 instance를 반환 (비어있을 때 Exception 발생)
E peek() 컬렉션의 처음 instance를 반환 (비어있을 때 null 반환)
first last
2
1 3
element()
peek()
1 반환
first last
null
element()
peek() null 반환
18. II. java.util.LinkedList CRUD
2) LinkedList CRUD
18
유튜브 - https://youtu.be/ZD4hukMaHrw
Return Type Method Description
E remove() 컬렉션의 처음 instance를 지우고 반환 (비어있을 때 Exception 발생)
E poll() 컬렉션의 처음 instance를 지우고 반환 (비어있을 때 null 반환)
start last
2
1 3
start
2
1 3
remove()
poll()
last