3
Recently I wrote a reply exploring a little more the API of stream
of Java 8, but I must say I was disappointed with some details of my code.
The idea was to read a sequence of numbers and then return what was the highest and lowest value of the readings taken. In this case, the entry ended when the number 0 was entered.
I don’t know how to skillfully use an iterable with the Apis of stream
. So I decided to go by the pig method and goal: I can read dice and play in a ArrayList
to then use this list quietly to stream
. The resulting code was this:
Scanner input = new Scanner(System.in);
ArrayList<Double> lista = new ArrayList<>();
double valorLido;
while (true) {
valorLido = input.nextDouble();
if (valorLido == 0) {
break;
}
lista.add(valorLido);
}
DoubleSummaryStatistics summary = lista.stream().collect(Collectors.summarizingDouble(Double::doubleValue));
double maior = summary.getMax();
double menor = summary.getMin();
System.out.println("maior " + maior);
System.out.println("menor " + menor);
This code did not please me. I would like to be able to directly use the reading result to pass to stream
no need to transform into any kind of collection.
For example, if I were to obtain an eternal reading, I would do so:
Iterable<Double> leitorDoubleAteh0(Scanner in) {
return () -> new Iterator<Double>() {
boolean conseguiuFetch;
double valorFetch;
void fetchNext() {
valorFetch = in.nextDouble();
conseguiuFetch = valorFetch != 0;
}
@Override
public boolean hasNext() {
return conseguiuFetch;
}
@Override
public Double next() {
if (!conseguiuFetch) {
throw new NoSuchElementException("fim da leitura");
}
double valorRetorno = valorFetch;
fetchNext();
return valorRetorno;
}
{
fetchNext();
}
};
}
That would at least give me access to foreach
, but still it is not my desired stream
.
I don’t know if I fully understood the question, but if the idea is how to go directly to
min
andmax
can do without collecting withlista.stream().max(Double::compare).get()
since themax
andmin
of the stream take a Comparator– Isac
Fair, but that doesn’t explain how to turn a reading sequence into a stream without going through a collection.
– Jefferson Quesado
The big problem here is that reading from the console you don’t get an EOF. A solution with a number of known lines is elegant but helps you?
– Juliano Alves
@Julianoalves known and limited number would not be my focus. The intention is to avoid loading an entire list into memory. I would like to work with stream using
o(1)
memory, noo(n)
– Jefferson Quesado