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.
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());
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.
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