본문 바로가기

JAVA

스트림11 스트림의 collector로 reducing 그룹별 리듀싱

스트림의 reducing 

 

Collector reducing (BinaryOperator<T> op)

Collector reducing (T Identity, BinaryOperator<T> op)

Collecotr reducing (U Identity , Function<T,U> mapper , BinaryOperator<U> op) //map + reduce

 

IntStream intStream = new Random().ints(1,46).distinct().init(6);

OptionalInt max = intStream.reduce(integer::max); //전체 리듀싱

OptionalInt<Integer> max = intStream.boxed().collect(reducing(Integer::max)) // 그룹별

 

Long sum = intStream.reduct (0,(a,b) -> a+b)

Long sum = intStream.boxed().collect(reducing(0,(a,b) -> a+b))

 

int grandTotal = stuStream.map(student::getTotalScore).reduce(o.Integer::sum);

int grandTotal = stuStream.collect(reducing(0, student::getTotalScore , Integer::sum));

 

문자열 스트림 모두 연결 joining

String studentNames = stuStream.map(student::getName).collect(joining());

String studentNames = stuStream.map(student::getName).collect(joining(",")) //구분자

String studentNames = stuStream.map(studnet::getName).collect(joining("," , "[","]"))

String studentInfo = stuStream.collect(joining(","))

//student의 toString()으로 결합.

 

 

'JAVA' 카테고리의 다른 글

스트림12 스트림의 그룹화 분할  (0) 2023.03.17
스트림10 최종연산 collect 와 colltors  (0) 2023.03.16
스트림9 최종연산 reduce()  (0) 2023.03.16
스트림8 최종연산 forEach()  (0) 2023.03.16
Optional<T>  (0) 2023.03.16