I am adding screenshot for some more information. I am new to Java8, kindly forgive me if I am asking a bad question. When I was doing research for input of space separated value I got this statement.
I understand that below statement takes value as 1 2 3
and returns value[1, 2, 3]
as a list.
Kindly correct me if I am wrong,
Stream: is used on collection for iteration.
bufferedReader.readLine(): it reads the line as string
,
later it gets parse into Integer and being collected into ListMy doubt is when I am inputting 1 2 3
which is a string. So, is this stream first splitting the value with space then iterating on splitted value? or is it 1st parsing the value later storing the value in list later splitting value with space and removing space and replacing with ,
. I am very curious, how exactly below statement is working.
List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
[1]: https://i.stack.imgur.com/nlXxd.jpg
The readLine() method of BufferedReader class in Java is used to read one line text at a time. The end of a line is to be understood by '\n' or '\r' or EOF.
The readLine() method of Console class in Java is used to read a single line of text from the console. Syntax: public String readLine() Parameters: This method does not accept any parameter. Return value: This method returns the string containing the line that is read from the console.
Java BufferedReader readLine() Method The readLine() method of Java BufferedReader class reads a line of text. The "/n" and "/r" are line termination character which is used to consider a line.
The BufferedReader. readLine() method returns null when it reaches the end of file. Your program appears to be reading line in the file before it reaches to the end of the file. The condition for terminating is that line is null, empty string is not null so it loop and dont get terminated.
bufferedReader.readLine()
reads one line, as a String
. The string isn't assigned to any variable.replaceAll("\\s$", "")
is anchored at the end of the line and removes the last whitespace. Using String#trim
would have been better.split(" ")
splits the string into a String[]
, breaking it apart at whitespacesStream.of(String[])
takes the String[]
we now have and returns a Stream<String>
Stream<String>.map(Function<String, Integer>)
converts the Stream<String>
into a Stream<Integer>
by applying the mapping function (Integer::parseInt
) to each element of the streamStream<Integer>#collect(toList())
takes a String<Integer>
and collects it into a List<Integer>
List<Integer> a = ...
assigns the result of the aforementioned steps to the variable a
And then you are done.
The comma you are seeing when using System.out.println(a)
are produced by calling List#toString()
(actually, AbstractCollection#toString
, which is inherited by most List implementations via Abstractlist
), which produces a comma-separated list of values, enclosed in square brackets. This is a string representation for easier readability by us humans and has nothing to do with how the list is stored internally. In an ArrayList
for example, the internal storage would be Integer[]
, other list implementations use other data structures.
Some debugger similarly show the contents of lists and arrays as such comma-separated lists - this is again purely for easier readability.
You say that the
.replaceAll("\\s$", "")
replaces a space with a comma, but that is not what it does. It replaces all occurences of a whitespace at the end of a string with no nothing. (\s
is any whitespace and $
is for end of string).
After this the string is split into an array, separator being a space: " ". Then all cells are parsed into integers and collected as a list.
The replace seems a bit weird, but I cannot say if is necessary or not without knowing the context. It looks like that trim would fit better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With