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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import java.util.*; import java.util.stream.*; import static java.util.stream.Collectors.*; import static java.util.Comparator.*; class Student3{ public Student3(String name, boolean sex, int hak, int ban, int score) { this.name = name; this.sex = sex; this.hak = hak; this.ban = ban; this.score = score; } public String getName() { return name; } public boolean isSex() { return sex; } public int getHak() { return hak; } public int getBan() { return ban; } public int getScore() { return score; } String name; boolean sex; int hak; int ban; int score; public String toString(){ return String.format("[%s, %s, %d학년, %d반, %3d점]",name, sex ? "남":"여" ,hak,ban,score); } //groupingBy사용 enum Level {HIGH, MID, LOW} //성적을 상,중,하 세 단계로 분류 } public class StreamEx7 { public static void main(String[] args) { Student3[] stuArr = { new Student3("권영미", false, 1, 1, 350), new Student3("박진영", true, 1, 1, 320), new Student3("김서현", false, 1, 1, 300), new Student3("이지현", false, 1, 2, 280), new Student3("이남의", false, 1, 2, 200), new Student3("조예은", false, 1, 2, 270), new Student3("김한솔", true, 1, 3, 240), new Student3("우지원", true, 1, 3, 70), new Student3("조현우", true, 1, 3, 100), new Student3("김기태", true, 1, 3, 330), new Student3("정하준", true, 1, 3, 200), new Student3("권영미", false, 2, 1, 350), new Student3("박진영", true, 2, 1, 320), new Student3("김서현", false, 2, 1, 300), new Student3("이지현", false, 2, 2, 280), new Student3("이남의", false, 2, 2, 200), new Student3("조예은", false, 2, 2, 270), new Student3("김한솔", true, 2, 3, 240), new Student3("우지원", true, 2, 3, 70), new Student3("조현우", true, 2, 3, 100), new Student3("김기태", true, 2, 3, 330), new Student3("정하준", true, 2, 3, 200) }; System.out.println("1. 단순 분할(성별로 분할)"); Map<Boolean,List<Student3>> stuBySex = Stream.of(stuArr).collect(partitioningBy(Student3::isSex)); //리스트로 넣은 다음 출력해주기 위함 List<Student3> maleStudent = stuBySex.get(true); List<Student3> femaleStudent = stuBySex.get(false); for(Student3 s : maleStudent) System.out.println(s); for(Student3 s : femaleStudent) System.out.println(s); System.out.println("%n2. 단순분할 + 통계(성별 학생수를 내놔라) %n "); Map<Boolean,Long> stuNumBySex = Stream.of(stuArr).collect(partitioningBy(Student3::isSex,counting())); System.out.println("남학생 수 :"+ stuNumBySex.get(true)); System.out.println("여학생 수 :"+ stuNumBySex.get(false)); System.out.println("%n3. 단순분할 + 통계(성별 1등) %n"); Map<Boolean,Optional<Student3>> topScoreBySex = Stream.of(stuArr).collect(partitioningBy(Student3::isSex,maxBy(comparingInt(Student3::getScore)))); //Optional이 같이 출력됨 System.out.println("남학생 1등 : "+ topScoreBySex.get(true)); System.out.println("여학생 1등 : "+ topScoreBySex.get(false)); //Optional을 Map에 안쓰고 그냥 Student3바로 쓸때 Map<Boolean,Student3> topScoreBySex2 = Stream.of(stuArr) .collect(partitioningBy(Student3::isSex ,collectingAndThen(maxBy(comparingInt(Student3::getScore)),Optional::get))); //?? 이게 제대로 출력되는데 왜 이러는지 모름 System.out.println("남학생 1등 : " + topScoreBySex2.get(true)); System.out.println("여학생 1등 : " + topScoreBySex2.get(false)); System.out.println("%n4. 다중분할(성별 불합격자, 100점 이하)%n"); Map<Boolean, Map<Boolean,List<Student3>>> failedStuBySex = Stream.of(stuArr).collect(partitioningBy(Student3::isSex, partitioningBy(s -> s.getScore() <= 100)) ); List<Student3> failedMaleStu = failedStuBySex.get(true).get(true); List<Student3> failedFemaleStu = failedStuBySex.get(false).get(true); for(Student3 s : failedMaleStu) System.out.println(s); for(Student3 s : failedFemaleStu) System.out.println(s); } } | cs |
1. 단순 분할(성별로 분할)
[박진영, 남, 1학년, 1반, 320점]
[김한솔, 남, 1학년, 3반, 240점]
[우지원, 남, 1학년, 3반, 70점]
[조현우, 남, 1학년, 3반, 100점]
[김기태, 남, 1학년, 3반, 330점]
[정하준, 남, 1학년, 3반, 200점]
[박진영, 남, 2학년, 1반, 320점]
[김한솔, 남, 2학년, 3반, 240점]
[우지원, 남, 2학년, 3반, 70점]
[조현우, 남, 2학년, 3반, 100점]
[김기태, 남, 2학년, 3반, 330점]
[정하준, 남, 2학년, 3반, 200점]
[권영미, 여, 1학년, 1반, 350점]
[김서현, 여, 1학년, 1반, 300점]
[이지현, 여, 1학년, 2반, 280점]
[이남의, 여, 1학년, 2반, 200점]
[조예은, 여, 1학년, 2반, 270점]
[권영미, 여, 2학년, 1반, 350점]
[김서현, 여, 2학년, 1반, 300점]
[이지현, 여, 2학년, 2반, 280점]
[이남의, 여, 2학년, 2반, 200점]
[조예은, 여, 2학년, 2반, 270점]
%n2. 단순분할 + 통계(성별 학생수를 내놔라) %n
남학생 수 :12
여학생 수 :10
%n3. 단순분할 + 통계(성별 1등) %n
남학생 1등 : Optional[[김기태, 남, 1학년, 3반, 330점]]
여학생 1등 : Optional[[권영미, 여, 1학년, 1반, 350점]]
남학생 1등 : [김기태, 남, 1학년, 3반, 330점]
여학생 1등 : [권영미, 여, 1학년, 1반, 350점]
%n4. 다중분할(성별 불합격자, 100점 이하)%n
[우지원, 남, 1학년, 3반, 70점]
[조현우, 남, 1학년, 3반, 100점]
[우지원, 남, 2학년, 3반, 70점]
[조현우, 남, 2학년, 3반, 100점]
'자바 이론 > 스트림' 카테고리의 다른 글
Optional<T>와 OptionalInt / NULL대신에 Optional (0) | 2019.04.16 |
---|---|
스트림의 중간연산 + 자바 CompareTo메서드 (Comparable) (0) | 2019.04.15 |
스트림 만들기 (0) | 2019.04.15 |
스트림 중간연산,최종연산 목록 (0) | 2019.04.15 |