JAVA chapter16. 스트림과 병렬처리. 16.11 수집(collect()) // 27분 // 추가
JAVA/CONCEPT 2018. 1. 16. 16:25 |JAVA chapter16. 스트림과 병렬처리.
16.11 수집(collect())
16.11.1 필터링한 요소 수집
Student.java
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 | package sec11.stream_collect; public class Student { public enum Sex { MALE, FEMALE } public enum City { Seoul, Pusan } private String name; private int score; private Sex sex; private City city; public Student(String name, int score, Sex sex) { this.name = name; this.score = score; this.sex = sex; } public Student(String name, int score, Sex sex, City city) { this.name = name; this.score = score; this.sex = sex; this.city = city; } public String getName() { return name; } public int getScore() { return score; } public Sex getSex() { return sex; } public City getCity() { return city; } } | cs |
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 31 32 33 34 35 36 | package sec11.stream_collect; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class ToListExample { public static void main(String[] args) { List<Student> totalList = Arrays.asList( new Student("홍길동", 10, Student.Sex.MALE), new Student("김수애", 6, Student.Sex.FEMALE), new Student("신용권", 10, Student.Sex.MALE), new Student("박수미", 6, Student.Sex.FEMALE) ); //남학생들만 묶어 List 생성 List<Student> maleList = totalList.stream() .filter(s->s.getSex()==Student.Sex.MALE) .collect(Collectors.toList()); maleList.stream() .forEach(s -> System.out.println(s.getName())); System.out.println(); //여학생들만 묶어 HashSet 생성 Set<Student> femaleSet = totalList.stream() .filter(s -> s.getSex() == Student.Sex.FEMALE) //.collect(Collectors.toCollection(HashSet :: new)); //.collect( Collectors.toCollection(()->{return new HashSet<Student>();}) ); .collect( Collectors.toCollection(()->new HashSet<Student>()) ); femaleSet.stream() .forEach(s -> System.out.println(s.getName())); } } | cs |
16.11.2 사용자 정의 컨테이너에 수집하기
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 | package sec11.stream_collect; import java.util.ArrayList; import java.util.List; public class MaleStudent { private List<Student> list; // 요소를 저장할 컬레션 public MaleStudent() { list = new ArrayList<Student>(); System.out.println("[" + Thread.currentThread().getName() + "] MaleStudent()"); } // 생성자를 호출하는 스레드 이름 public void accumulate(Student student) { // 요소를 수집하는 메소드 list.add(student); System.out.println("[" + Thread.currentThread().getName() + "] accumulate()"); } // accumulate()를 호출할 스레드 이름 public void combine(MaleStudent other) { // 두 MaleStudent를 결합하는 메소드 list.addAll(other.getList()); // (병렬 처리 시에만 호출) System.out.println("[" + Thread.currentThread().getName() + "] combine()"); } // combine()을 호출한 스레드 이름 public List<Student> getList() { // 요소가 저장된 컬렉션을 리턴 return list; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package sec11.stream_collect; import java.util.Arrays; import java.util.List; public class MaleStudentExample { public static void main(String[] args) { List<Student> totalList = Arrays.asList( new Student("홍길동", 10, Student.Sex.MALE), new Student("김수애", 6, Student.Sex.FEMALE), new Student("신용권", 10, Student.Sex.MALE), new Student("박수미", 6, Student.Sex.FEMALE) ); MaleStudent maleStudent = totalList.stream() .filter(s -> s.getSex() == Student.Sex.MALE) //.collect(MaleStudent :: new, MaleStudent :: accumulate, MaleStudent :: combine); .collect(()->new MaleStudent(), (r, t)->r.accumulate(t), (r1, r2)->r1.combine(r2)); maleStudent.getList().stream() .forEach(s -> System.out.println(s.getName())); } } | cs |
16.11.3 요소를 그룹핑해서 수집
16.11.4 그룹핑 후 매핑 및 집계
'JAVA > CONCEPT' 카테고리의 다른 글
JAVA chapter17. javaFX. 17.8 JavaFX 메뉴바와 툴바 (0) | 2018.01.16 |
---|---|
JAVA chapter16. 스트림과 병렬처리. 16.12 병렬 처리 (0) | 2018.01.16 |
JAVA chapter16. 스트림과 병렬처리. 16.10 커스텀 집계(reduce()) (0) | 2018.01.16 |
JAVA chapter16. 스트림과 병렬처리. 16.9 기본 집계(sum(), count(), average(), max(), min()) (0) | 2018.01.16 |
JAVA chapter16. 스트림과 병렬처리. 16.8 매칭(allMach(), anyMatch(), noneMatch()) (0) | 2018.01.16 |