
collect() 메서드(3) - groupingBy()
·
자바/스트림
1. groupingBy()에 의한 분류 - 아래의 코드처럼 그룹화를 하면 기본적으로 List에 담으며, toList()를 생략한다. Map stuByBan = Stream.of(stuArr) .collect(groupingBy(Stu3::getBan, toList())); // toList()가 생략 가능 !! Map stuByHak = Stream.of(stuArr) .collect(groupingBy(Stu3::getHak, toCollection(HashSet::new)); ( 또한 toSet()을 사용할 수도 있다. ) ( 아래의 Student의 정보가 있는 stu3클래스가 정의되어 있다고 가정하고 아래의 예제를 수행할 것이다. ) (1) 반별 기준으로 그룹화하기. (2) 성적별로 그룹화하기. ..