'How to convert "Stream<String>" to "String"
I want to convert Stream<String> to String and print all the string.
Stream<String> result= Stream.of("do","re","ma","po","fa","si")
.filter(str -> str.length()>5)
.peek(System.out:: println);
//.allMatch(str -> str.length() >5);
System.out.println(result);
Here down output i get
java.util.Spliterators$1Adapter@63961c42
The result is printing an object but not a String and while converting to toString() also printing the same but how to print as a String
Solution 1:[1]
You have to store Stream<String> into the String. After that you have to join this string using Collectors.joining()
Here down is code:
Stream<String> streamVal= Stream.of("do","re","ma","po","fa","si");
String result = streamVal.filter(str -> str.length() > 1)
.collect(Collectors.joining());
System.out.println(result);
Solution 2:[2]
If you really only want to only print the elements rather than getting them in return, you can just
Stream.of("do","re","ma","po","fa","si")
.filter(str -> str.length()>5)
.forEach(System.out:: println);
forEach is also a "terminating" operation on the stream, which means that it "actually executes" it.
Solution 3:[3]
Do you looking for something like this?
String result = Stream.of("do","re","ma","po","fa","si").
collect(Collectors.joining(""));
System.out.println(result);
Output:
doremapofasi
Or:
String result = Stream.of("do", "re", "ma", "po", "fa", "si")
.filter(str -> str.length() > 1)
.peek(System.out::println)
.collect(Collectors.joining(""));
System.out.println(result);
Output:
do
re
ma
po
fa
si
doremapofasi
Solution 4:[4]
You misunderstand the mechanisms behind the Stream IPA.
A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate)), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer)).
Streams are lazy;computationon the source data isonly performed when the terminal operation is initiated, and source elements are consumed only as needed.
The main conclusion:
Operations on the data in the pipeline will be performed only if the terminal operation is initiated.
Stream without a terminal operation is perfectly valid from the compiler's perspective of view. It will compile, but it'll not be executed.
And what you have tried to print (java.util.Spliterators$1Adapter@63961c42) isn't a result, but the stream object itself.
To produce a result or side-effect Stream-pipeline must end with a terminal operation (collect(), reduce(), count(), forEach(), findFirst(), findAny, anyMatch() - which commented out in your code). Note, that peek() is an intermediate operation and confuse it with forEach(). You can use peek() as many time as need, which can be useful for debugging purposes.
public static void main(String[] args) {
String result = getStream()
.filter(str -> str.length() > 5 && str.length() < 8)
.findFirst() // that will produce a single result that may or may not be present
.orElseThrow(); // action for the case if result is not present
System.out.println("Single result: " + result + "\n");
getStream()
.filter(str -> str.contains("a"))
.peek(System.out::println) // intermediate operation that will print every element that matches the first filter
.filter(str -> str.length() > 8)
.forEach(System.out::println); // terminal operation that prints every element remained after processing the pipeline
}
private static Stream<String> getStream() {
return Stream.of("Ignoranti", "quem", "portum", "petat", "nullus", "suus", "ventus", "est");
}
output
Single result: portum
Ignoranti
Ignoranti
petat
Solution 5:[5]
you need to 'execute' the stream by calling collect or reduce
assertEquals("ab", Arrays.stream(new String[]{"a", "b"}).reduce("", (s,x) -> s + x));
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 | |
| Solution 2 | daniu |
| Solution 3 | IvanMikhalka |
| Solution 4 | |
| Solution 5 | Simone Caruso |
