Trying to understand the complete meaning of a below statement Stream.of(bufferedReader.readLine().replaceAll("\\s$", "")

Viewed 1704

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

  • replaceAll: is replacing space with , later it gets parse into Integer and being collected into List

My 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
2 Answers
  • 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 whitespaces
  • Stream.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 stream
  • Stream<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.

Related