Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 가상 채팅서버
- 스프링 핵심원리 기본편
- 스프링 MVC 2편
- 채팅서버 설계
- 스파르타코딩클럽
- 스프링 시큐리티 구조
- SQL
- 스프링심화반
- 알고리즘
- 타임리프
- 객체지향의 사실과 오해
- 누구나큰일낼수있어
- 백준
- 김영한님
- 스파르타코딩클럽 #spartacodingclub #누구나큰일낼수있어
- C++
- 백준 1992번
- 전문가를 위한 스프링5
- 스프링
- 팀300
- thymeleaf
- 1시간 만에 끝내는 직장인 코딩 용어
- 스프링 입문을 위한 자바 객체 지향의 원리와 이해
- JPA
- 백준 2630번
- 백준 #N과 M(4) #백트래킹
- spartacodingclub
- 백준 1992번 풀이
- 전문가를 위한 스프링
- 자바의 정석 기초편
Archives
- Today
- Total
민우의 코딩노트
스트림의 그룹화와 분할 본문
https://www.youtube.com/watch?v=VUh_t_j9qjE&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp&index=171
본 강의는 위 강의를 듣고 정리를 진행했습니다.
- partitioningBy()는 스트림을 2분할 한다.
- groupingBy()는 스트림을 n분할 한다.
Collector partitioingBy(Predicate predicate)
Map<Boolean, List<Student>> stuBySex = stuStream.collect(paritioingBy(Student::isMale));
List<Student> maleStudent = stuBySex.get(true);
List<Student> femaleStudent = stuBySex.get(false);
Map<Boolean, Long> stuNumBySex = stuStream.collect(partitioningBy(Student::isMale, counting()));
Long maleCount = stuBySex.get(true);
List femaleCount = femaleStudent = stuBySex.get(false);
// 분할 + 통계
Map<Boolean, Optional<Student>> topScoreBySex = stuStream.collect(partitioningBy(Student::isMale, maxBy(comparingInt(Student::getScore));
// 남학생 1등, 여학생 1등 출력
topScoreBySex.get(true), topScoreBySex.get(false);
// 다중 분할
Map<Boolean, Map<Boolean, List<Student>>> failedStuBySex = stuStream // 다중 분할
.collect(partitioningBy(Student::isMale, paritioningBy(s -> s.getScore() < 150))); // 성별, 성적 분할
List<Student> failedMaleStu = failedStuBySex.get(true).get(true);
- 스트림의 그룹화 - groupingBy()
- 스트림의 요소를 그룹화
Collector groupingBy(Function classifier)
Map<Integer, List<Student>> stuByBan = stuStream.collect(groupingBy(Student::getBan, toList()));
// 학생 반별 그룹화 toList 생략 가능
// 다중 그룹화 1. 학년 그룹화 2. 반별 그룹화
Map<Integer, Map<Integer, List<Student>>> stuByHakAndBan = stuStream.collect(
groupingBy(Student::getHak, groupingBy(Student::getBan)));
Map<Integer, Map<Integer, Set<Student.Level>>> stuByHakAndBan = stuStream
.collect(
groupingBy(Student::getHak, groupingBy(Student::getBan,
mapping(s -> { // 성적 등급으로 변환(level)
if (s.getScore() >= 200) return Student.Level.HIGH;
else if(s.getScore() >= 100) return Student.Level.MID;
else return Student.Level.LOW;
}, toSet()) // mapping // enum Level {HIGH, MID, LOV}}
)) // GROUPING BY
}; // collect()
'Book > 자바의 정석 - 기초편' 카테고리의 다른 글
선언위치 변수종류(클래스 변수/인스턴스 변수) (0) | 2024.01.04 |
---|