K
K
kvasek2016-07-21 14:49:08
Java
kvasek, 2016-07-21 14:49:08

In Java, when working with filter, map, etc. you need to call the stream() method. Why and why is it designed this way?

Hello everyone,
I'm not a Java developer but I'm interested in this question.
As I understand it, the Java Stream API is just a really cool bunch of syntactic sugar and functionality.
Most languages ​​such as Ruby, C#, and others do well without this method. If we need to filter the collection, we simply apply the filter without streams, and this is convenient and understandable.
I'm wondering why the designers of Java added the stream() method?
From my side it looks like a crutch.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Толстый Лорри, 2016-07-21
@kvasek

В Java нет методов расширения, потому на коллекцию можно "навешивать" функциональность аналогичным образом (C#/LINQ), оборачивая коллекцию в Stream<T>.

G
guras256, 2016-07-26
@guras256

stream is a recipe for streaming data. in order for it to do anything, a terminal operation is needed. it's done for laziness.
for example:
listOf(1,2,3,4,5).stream().filter(it -> it > 3).map(it -> it + 1)
this line will not do anything, since the stream is not closed ( no terminal operation)
listOf(1,2,3,4,5).stream().filter(it -> it > 3).map(it -> it + 1).collect(toList())
now has a terminal the collect operation and the string will work as expected.
and all this will be performed lazily (in one cycle), that is, we will not bypass the list 2 times, we will bypass it 1 time sequentially applying operations
in this case, the element is checked for> 3, increases by 1, the next number is checked for> 3. increases by 1 and so on.

Роман, 2016-09-30
@Terran37

Посмотрите, все описал, что зачем и почему. javarules.ru/stream-api

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question