collect(partitioningBy(Predicate))
=> true 값과 false로 나누어서 반환
collect(Collectors.groupingBy(Function))
=> Function에서 리턴값으로 그룹핑해서 반환
8. 성별 분류
//익명클래스
Predicate<Stu> predicate = new Predicate<Stu>() {
@Override
public boolean test(Stu t) {
return t.isMale();
}
};
Map<Boolean, List<Stu>> xxx = Stream.of(stuArr).collect(Collectors.partitioningBy(predicate));
List<Stu> male = xxx.get(true);
List<Stu> female = xxx.get(false);
System.out.println("male: " + male);
System.out.println("female: " + female);
//male: [Stu [username=홍길동, grade=3, isMale=true, age=25], Stu [username=이순신, grade=1, isMale=true, age=55], Stu [username=안중근, grade=3, isMale=true, age=31], Stu [username=홍범도, grade=1, isMale=true, age=58], Stu [username=강감찬, grade=3, isMale=true, age=44]]
//female: [Stu [username=유관순, grade=2, isMale=false, age=18]]
//람다표현식
Map<Boolean, List<Stu>> xxx2 =
Stream.of(stuArr)
// .collect(Collectors.partitioningBy(t->t.isMale()));
.collect(Collectors.partitioningBy(Stu::isMale)); //method reference
List<Stu> male2 = xxx2.get(true);
List<Stu> female2 = xxx2.get(false);
System.out.println("male2: " + male2);
System.out.println("female2: " + female2);
//male2: [Stu [username=홍길동, grade=3, isMale=true, age=25], Stu [username=이순신, grade=1, isMale=true, age=55], Stu [username=안중근, grade=3, isMale=true, age=31], Stu [username=홍범도, grade=1, isMale=true, age=58], Stu [username=강감찬, grade=3, isMale=true, age=44]]
//female2: [Stu [username=유관순, grade=2, isMale=false, age=18]]
9. grade 그룹핑
//익명클래스
Function<Stu, Integer> classifier = new Function<Stu, Integer>(){
@Override
public Integer apply(Stu t) {
return t.getGrade();
}
};
Map<Integer, List<Stu>> yyy = Stream.of(stuArr)
.collect(Collectors.groupingBy(classifier));
Set<Integer> keys3 = yyy.keySet();
for (Integer key: keys3) {
System.out.println(key+"=" + yyy.get(key));
}
//1=[Stu [username=이순신, grade=1, isMale=true, age=55], Stu [username=홍범도, grade=1, isMale=true, age=58]]
//2=[Stu [username=유관순, grade=2, isMale=false, age=18]]
//3=[Stu [username=홍길동, grade=3, isMale=true, age=25], Stu [username=안중근, grade=3, isMale=true, age=31], Stu [username=강감찬, grade=3, isMale=true, age=44]]
grade 그룹핑 갯수 구하기
Map<Integer, Long> yyy2 = Stream.of(stuArr)
.collect(Collectors.groupingBy(classifier, Collectors.counting()));
Set<Integer> keys4 = yyy2.keySet();
for (Integer key: keys4) {
System.out.println(key+"=" + yyy2.get(key));
}
//1=2
//2=1
//3=3
'Programming Language > JAVA' 카테고리의 다른 글
자바 스트림 API - Optional (0) | 2023.09.08 |
---|---|
자바 스트림 API - 최종처리(3)_collect (0) | 2023.09.08 |
자바 스트림 API - 최종처리(2)_IntStream, LongStream, DoubleStream의 집계메서드 (0) | 2023.09.08 |
자바 스트림 API - 최종처리_allMatch/anyMatch/noneMatch (0) | 2023.09.08 |
자바 스트림 API- 중간처리(3)_boxed/asDoubleStream() (0) | 2023.09.07 |
collect(partitioningBy(Predicate))
=> true 값과 false로 나누어서 반환
collect(Collectors.groupingBy(Function))
=> Function에서 리턴값으로 그룹핑해서 반환
8. 성별 분류
//익명클래스
Predicate<Stu> predicate = new Predicate<Stu>() {
@Override
public boolean test(Stu t) {
return t.isMale();
}
};
Map<Boolean, List<Stu>> xxx = Stream.of(stuArr).collect(Collectors.partitioningBy(predicate));
List<Stu> male = xxx.get(true);
List<Stu> female = xxx.get(false);
System.out.println("male: " + male);
System.out.println("female: " + female);
//male: [Stu [username=홍길동, grade=3, isMale=true, age=25], Stu [username=이순신, grade=1, isMale=true, age=55], Stu [username=안중근, grade=3, isMale=true, age=31], Stu [username=홍범도, grade=1, isMale=true, age=58], Stu [username=강감찬, grade=3, isMale=true, age=44]]
//female: [Stu [username=유관순, grade=2, isMale=false, age=18]]
//람다표현식
Map<Boolean, List<Stu>> xxx2 =
Stream.of(stuArr)
// .collect(Collectors.partitioningBy(t->t.isMale()));
.collect(Collectors.partitioningBy(Stu::isMale)); //method reference
List<Stu> male2 = xxx2.get(true);
List<Stu> female2 = xxx2.get(false);
System.out.println("male2: " + male2);
System.out.println("female2: " + female2);
//male2: [Stu [username=홍길동, grade=3, isMale=true, age=25], Stu [username=이순신, grade=1, isMale=true, age=55], Stu [username=안중근, grade=3, isMale=true, age=31], Stu [username=홍범도, grade=1, isMale=true, age=58], Stu [username=강감찬, grade=3, isMale=true, age=44]]
//female2: [Stu [username=유관순, grade=2, isMale=false, age=18]]
9. grade 그룹핑
//익명클래스
Function<Stu, Integer> classifier = new Function<Stu, Integer>(){
@Override
public Integer apply(Stu t) {
return t.getGrade();
}
};
Map<Integer, List<Stu>> yyy = Stream.of(stuArr)
.collect(Collectors.groupingBy(classifier));
Set<Integer> keys3 = yyy.keySet();
for (Integer key: keys3) {
System.out.println(key+"=" + yyy.get(key));
}
//1=[Stu [username=이순신, grade=1, isMale=true, age=55], Stu [username=홍범도, grade=1, isMale=true, age=58]]
//2=[Stu [username=유관순, grade=2, isMale=false, age=18]]
//3=[Stu [username=홍길동, grade=3, isMale=true, age=25], Stu [username=안중근, grade=3, isMale=true, age=31], Stu [username=강감찬, grade=3, isMale=true, age=44]]
grade 그룹핑 갯수 구하기
Map<Integer, Long> yyy2 = Stream.of(stuArr)
.collect(Collectors.groupingBy(classifier, Collectors.counting()));
Set<Integer> keys4 = yyy2.keySet();
for (Integer key: keys4) {
System.out.println(key+"=" + yyy2.get(key));
}
//1=2
//2=1
//3=3
'Programming Language > JAVA' 카테고리의 다른 글
자바 스트림 API - Optional (0) | 2023.09.08 |
---|---|
자바 스트림 API - 최종처리(3)_collect (0) | 2023.09.08 |
자바 스트림 API - 최종처리(2)_IntStream, LongStream, DoubleStream의 집계메서드 (0) | 2023.09.08 |
자바 스트림 API - 최종처리_allMatch/anyMatch/noneMatch (0) | 2023.09.08 |
자바 스트림 API- 중간처리(3)_boxed/asDoubleStream() (0) | 2023.09.07 |