Get minimum and maximum supplier flows

I know if I use:

double data[][] = new double[n][];
// fill the array

DoubleStream stream = Arrays.stream(data).flatMapToDouble(Arrays::stream);
int max = stream.max().getAsDouble();

DoubleStream stream = Arrays.stream(data).flatMapToDouble(Arrays::stream);
int min = stream.min().getAsDouble();

i will get the minimum and maximum value that the stream has, like double.

However, I can’t understand for my whole life how to turn it into a “Supplier”. So

Supplier<Stream> stream = (Supplier<Stream>) Arrays.stream(data).flatMapToDouble(Arrays::stream);
double max = stream.max().getAsDouble();
double min = stream.min().getAsDouble();

Supplier<DoubleStream>ether does not work, but ether does not work.

I managed to get it to work with

Supplier<DoubleStream> stream = () -> Arrays.stream(t).flatMapToDouble(Arrays::stream);
OptionalDouble max = stream.get().max();
OptionalDouble min = stream.get().min();

but why did the values ​​in the stream become OptionDouble?

+4
source share
2 answers

Listing DoubleStreamon Supplier<DoubleStream>does not DoubleStreama Supplier<DoubleStream>.

To form a flow provider, a lambda expression is required () -> stream:

Supplier<DoubleStream> supplier = () -> Arrays.stream(data).flatMapToDouble(Arrays::stream);

The maximum value can then be determined:

double max = supplier.get().max().orElse(Double.MIN_VALUE); 
// where Double.MIN_VALUE can be any other default 'double' value if max is not present

as max()above DoubleStreamreturns OptionalDouble.

+3

Supplier<T> :

@FunctionalInterface
public Supplier<T> {

    T get();
}

, - ( ) Supplier:

Supplier<Integer> supplier = () -> 1;

Integer DoubleStream, , :

Supplier<DoubleStream> supplier = Arrays.stream(data).flatMapToDouble(Arrays::stream);

: DoubleStream, Supplier<DoubleStream> , .

, , .

DoubleStream.max DoubleStream.min, OptionalDouble ( double), , , . OptionalDouble .

0

Source: https://habr.com/ru/post/1688690/


All Articles