[JAVA] Stream API (Collectors class)

What is Stream API?

Stream API is an API that performs aggregation operations based on data sources such as collections and arrays. This article describes the Collectors class, which is one of the termination operations. The Collectors class implements the Collector interface. The collect () method can collect elements from a stream into one object.

collect () method

(1) <R,A> R collect(Collector<? super T,A,R> collector) (2) <R> R collect(Supplier<R> supplier,BiConsumer<R ? superT> accumulator, BiConsumer<R,R> combiner)

Collectors class methods

(1) Collector<T,?,List<T>> toList() (2) Collector<T,?,Integer> summingInt() (3) Collector<T,?,Double> averagingInt()

Myclass.java


Stream<String> stream = Steam.of("aa","bb","cc");

//List streams with toList
List<String> result1 = stream.collect(Collectors.toList());
System.out.print(result1); //[aa,bb,cc]

//summingInt gives the sum of all the bundles
Integer result2 = stream.collect(Collectors.summingInt(x->x.length());
System.out.print(result2); //6

//averagingInt finds the mean of streams
Double result3 = stream.collect(Collectors.averagingInt(x->x.length()));
System.out.print(result2); //2.0

Summary

Regarding the Colectors class, it has the image of "execution processing as a whole".

Recommended Posts

Stream API (Collectors class)
Stream API memo
Java Stream API
Stream API basics
Anonymous class (aiming to introduce stream api)
[Java] Stream API / map
Stream API map method
Java8 Stream API practice
Java Stream API cheat sheet
Java Stream API in 5 minutes
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[Java] Introduction to Stream API
[Java] Stream API intermediate operation
[java8] To understand the Stream API
[Introduction to Java] About Stream API
Create an easy-to-extend Stream API alternative
I tried using Java8 Stream API
Java 8 ~ Stream API ~ to start now
Data processing using stream API from Java 8
Try using the Stream API in Java
Nowadays Java lambda expressions and Stream API
Try various Java Stream API methods (now)
I tried to summarize the Stream API
Notes on Java's Stream API and SQL