I have an input (STDIN)
0 0 1 2 1
and I want to create a stream from it in the easiest way.
I created a stream by reading one for each integer and storing them in an ArrayList. From there, I just had to use .stream (). Reduce () to make it work.
What I want is a possible way to create a stream directly from input
I tried to adapt this code:
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); IntStream is2 = IntStream.generate(inputStream::read).limit(inputStream.available());
using the DataInputStream and readInt () method
DataInputStream dis = new DataInputStream(System.in); IntStream is2 = IntStream.generate(dis::readInt).limit(5);//later : dis.available() instead of 5
but it does not work. I have incompatible thrown IOException types for the generate function.
Can I do it? Or is there another way to convert input to stream
my reduction function to be applied,
reduce( (x , y) -> x ^ y)
I already did this on an ArrayList quite easily by doing
list.stream().reduce( (x , y) -> x ^ y);
Decision
I tried using this with a scanner to do this job, but to no avail, and now I managed to get it to work
Scanner sc = new Scanner(System.in); int oi = Stream.of(sc.nextLine().split(" ")) .mapToInt(Integer::parseInt) .reduce( (x , y) -> x ^ y).getAsInt(); System.out.println(oi);
I do not understand why this did not work in the beginning