티스토리 뷰
프로그램을 코드를 작성 할 떄 데이터를 그룹화 하여 처리 하는 것이 전체 소스의 많은 부분이다., 자바에서 Collection , Map을 사용 해서 데이터를 그룹화 하고 제공하는 API를 이용해서 이 부분을 작성 한다, Java 1.8이상 부터는 데이터 처리 하는데 임시 구현 코드 대신 질의로 표현 하여 Collection 데이터를 처리 할 수 있는데 이것을 Stream이라 한다.
1. Collection Interface 특징
인터페이스 | 구현클래스 | 특징 |
Set | HashSet TreeSet |
순서를 유지하지 않는 데이터의 집합으로 데이터의 중복을 허용하지 않는다. |
List | LinkedList Vector ArrayList |
순서가 있는 데이터의 집합으로 데이터의 중복을 허용한다. |
Queue | LinkedList PriorityQueue |
List와 유사 |
Map | Hashtable HashMap TreeMap |
키(Key), 값(Value)의 쌍으로 이루어진 데이터으 집합으로, 순서는 유지되지 않으며 키(Key)의 중복을 허용하지 않으나 값(Value)의 중복은 허용한다. |
2. Stream
API 참고 : https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Stream (Java Platform SE 8 )
A sequence of elements supporting sequential and parallel aggregate operations. The following example illustrates an aggregate operation using Stream and IntStream: int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight())
docs.oracle.com
2-1. 특징
- 선언형 : 더 간결하고 가독성이 좋아진다.
- 조립 : 유연성이 좋아진다,
- 병렬화 : 성능이 좋아진다.
- 소비 : 단 한번만 소비 한다.
List<Integer> transactionsIds =
transactions.stream()
.filter(t -> t.getType() == Transaction.GROCERY)
.sorted(comparing(Transaction::getValue).reversed())
.map(Transaction::getId)
.collect(toList());
2-2.외부 반복과 내부 반복
- 외부 반복(external iteration) : Collection Interface를 사용해서 사용자가 직접 요소를 반복 ( for-each )
for( Car car : cars) { // cars를 명시적으로 반복
result.add(car.getName()); // 이름을 추출 하여 리스트에 추가
}
- 내부 반복(internal iteration) : Stream을 사용해서 반복을 내부에서 알아서 처리 하고 결과만 받는다.
List<String> names = cars.stream()
.map(Car::getName) // map 메서드르를 getName 메서드를 파라메터화 해서 이름 추출
.collect(Collectors.toList()); // 파이프라인 실행 ( 반복자 필요 없음
2-3. 스트림 연산
2-3-1. 스트림 이용 과정
- 질의를 수행할 소스
- 스트림 파이프라인을 구성랄 중간 연산 연결
- 스트림 파이프라인을 실행하고 결과를 만드는 최종 연산
- stateless : 상태정보를 저장하지 않는 형태
- statefull : 상태정보를 저장
참고 : https://www.geeksforgeeks.org/lambda-expressions-java-8/
Lambda Expressions in Java 8 - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
참고 : https://www.geeksforgeeks.org/stream-in-java/
Stream In Java - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
참고 : https://www.geeksforgeeks.org/java-8-stream-tutorial/
Java 8 Stream Tutorial - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
참고 : https://www.baeldung.com/java-collectors-tomap
Java 8 Collectors toMap | Baeldung
Learn how to use the toMap() method of the Collectors class.
www.baeldung.com
참고 : https://www.baeldung.com/java-stream-immutable-collection
Collect a Java Stream to an Immutable Collection | Baeldung
Learn how to collect Java Streams to immutable Collections.
www.baeldung.com