반응형
중간 연산의 종류
중간 연산 | 설명 | |
Stream<T> distinct() | 중복 제거. | |
Stream<T> filter(Predicate<T> predicate) | 조건에 맞지 않는 요소 제거. | |
Stream<T> limit(long maxSize) | 스트림 일부 잘라내기. | |
Stream<T> skip(long n) | 스트림 일부 건너뛰기. | |
Stream<T> peek(Consumer<T> action) | 작업 중간을 확인할 때 사용한다. | |
Stream<T> sorted() Stream<T> sorted(Comparator<T> comparator) |
요소 정렬 | |
Stream<R> DoubleStream IntStream LongStream |
map(Function<T, R> mapper) mapToDouble(ToDoubleFunction<T> mapper) mapToInt(ToIntFunction<T> mapper) mapToLong(ToLongFunction<T> mapper) |
스트림의 요소를 변환한다. |
Stream<R> DoubleStream IntStream LongStream |
flatMap(Function<T, Stream<R>> mapper) flatMapToDouble(Function<T,Doublestream> m) flatMapToInt(Function<T, Intstream> m) flatMapToLong(Function<T, Longstream> m) |
🌟 중간 연산은 모두 Stream을 반환합니다.
skip(long n)
: 앞에서부터 n개 건너뛰기.
limit(long maxSize)
: maxSize 이후의 요소는 잘라낸다.
distinct()
: 중복제거.
filter(조건)
: 조건에 맞지 않는 요소는 걸러낸다.
sorted(기준)
: 요소를 정렬한다.
🌟 sorted(Comparator.reverseOrder()) : 거꾸로 정렬한다.
sorted(람다식 or 메서드참조)
.sorted(String.CASE_INSENSITIVE_ORDER)
: 문자열을 정렬해준다.
(대소문자 구분❌)
🌟 거꾸로 정렬하고 싶을 때는 String.CASE_INSENSITIVE_ORDER.reversed()로 적어준다.
.sorted(Comparator.comparing(String::length))
: 문자열길이로 정렬하기.
💡 Comparator의 comparing(입력데이터 타입, 기준)
🌟 추가 정렬기준을 제공할 때는 thenComparing()을 사용한다.
- Comparator.comparing(Student::getBan)
→ 입력값은 Student이고, 기준 정렬은 ban이다. - 이후 추가로 정렬기준을 더하기 위해 thenComparing()을 사용하였다.
- thenComparing()의 기본정렬은 Student클래스에 정의된 compareTo()이다. (총점 높은 순)
반응형
'☕ JAVA > ☕ Class & Method' 카테고리의 다른 글
[JAVA]Optional<T> (0) | 2024.12.17 |
---|---|
[JAVA][Stream]중간 연산-map() peek() flatMap() (1) | 2024.12.16 |
[JAVA][Stream]스트림 생성 (1) | 2024.12.13 |
[JAVA][stream]스트림이란? (+특징) (0) | 2024.12.13 |
[JAVA]메서드 참조 method reference (0) | 2024.12.12 |