'How to use String methods on java.util.stream.Stream lines
I'd like to use the Stream API to compact some existing functions I have. I have a doubt on how to call functions on the lines I'm streaming. For example, take the following snippet:
String str = "abcdef";
Stream<String> lines = str.lines();
lines.forEach(System.out::println);
Is it possible to invoke String methods on each line? for example, instead of just printing the raw line, I want to print the toUpperCase() of it. So that the output is:
ABCDEF
Is there a way to plug the toUpperCase() function in this line:
lines.forEach(System.out::println);
Solution 1:[1]
Sure.
lines.forEach(line -> System.out.println(line.toUpperCase());
or
lines.map(String::toUpperCase).forEach(System.out::println);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Louis Wasserman |
