Stream API memo

One of the APIs added in Java 8 that allows you to write functional programming such as lambda expressions.

Stream collects collections, lists, etc. in one place and allows you to process them. At that time, when the intermediate processing and termination processing are completed, the processed result is returned so that the data can be handled more easily by oneself.

Stream example

In ElasticSearch source code

this.registry = new NamedXContentRegistry(
    Stream.of(getDefaultNamedXContents().stream(), getProvidedNamedXContents().stream(), namedXContentEntries.stream())
        .flatMap(Function.identity()).collect(toList()));   

Looking at the code of, I thought that this was somehow collecting something like a list and finally returning it as a list, but I understand it a little deeper for studying. I did some research to get it.

stream() Methods in List type etc. A method that creates a Stream type so that it can be used with a Stream type.

Arrays.asList("one", "two", "three").stream();
Arrays.asList(new Moment(), new Moment(), new Moment(), new Moment()).stream();

Stream.of() A method that can organize a Stream type list at once.

List<String> numbers = Arrays.asList("one", "two", "three");
Stream.of(numbers.stream(), numbers.stream(), numbers.stream());

flatMap() A method to combine the combined list into one list. You can describe the process using forEach etc. in this method.

Stream.of(list1.stream(), list1.stream(), list1.stream())
    .flatMap(t -> t).forEach(t -> System.out.println(t));  // "one", "two", "three", "one", "two", "three", "one", "two", "three"

Stream.of(list1.stream(), list1.stream(), list1.stream())
    .forEach(t -> System.out.println(t));  // reference values

Function.identity() A method that returns the value as is. At first I was wondering why there was such a thing, but there were many reasons why it was easier to read if I could write a method like t-> t like a method or Function :: identity.

Stream.of(numbers.stream(), numbers.stream(), numbers.stream())
    .flatMap(Funcion.indentity());

This stackoverflow was easy to understand.

collect() A method that returns a Collectors type when terminating a stream. You can decide which type (list, etc.) to return further within that method.

Stream.of(numbers.stream(), numbers.stream(), numbers.stream())
    .flatMap(Funcion.indentity())
    .collect(~);

Collectors.toList() A method that returns a Collectors type as a List type. This can be used within the collect method.

Stream.of(numbers.stream(), numbers.stream(), numbers.stream())
    .flatMap(Funcion.indentity())
    .collect(toList());

First code description

this.registry = new NamedXContentRegistry(
    Stream.of(getDefaultNamedXContents().stream(), getProvidedNamedXContents().stream(), namedXContentEntries.stream())
        .flatMap(Function.identity()).collect(toList()));     

Going back to the beginning, what the above code does is to make the List type Stream type, then put it together in Stream.of, and use flatMap to unify the contents of the List. After that, collect makes it into Collectors type, and toList finally makes it into List type and returns it (long).

It might have been better to have a collection rather than a list.

It's a punch line ...

Actually, I posted the site that I made to Qiita as it is.

I don't know in the future, but I will give it to Qiita.

Recommended Posts

Stream API memo
Memo Stream
Java Stream API
Stream API basics
Stream API (Collectors class)
[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
Niconico API memo (Let's see it)
Create an easy-to-extend Stream API alternative
I tried using Java8 Stream API
Memo for making Niconico API (Materials)
Java 8 ~ Stream API ~ to start now
Integer memo
Articles to learn more about Stream API
Rails API
Call API [Call]
Data processing using stream API from Java 8
Lombok memo
Try using the Stream API in Java
Dockerfile memo
Nowadays Java lambda expressions and Stream API
Iterator memo
corretto memo
Stream play
Java memo
AWS memo
Try various Java Stream API methods (now)
Stream termination
Anonymous class (aiming to introduce stream api)
I tried to summarize the Stream API
Notes on Java's Stream API and SQL
Dcokerfile memo
Ruby memo
[For beginners] About lambda expressions and Stream API
[Java] How to operate List using Stream API