[英]How to convert "Stream<String>" to "String"
I want to convert Stream<String>
to String
and print all the string.我想将
Stream<String>
转换为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这里下来 output 我得到
java.util.Spliterators$1Adapter@63961c42
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结果是打印
object
但不是String
,并且在转换为toString()
时也打印相同但如何打印为字符串
In java 8, there is method joining() , Which is the part of Collectors class .在 java 8 中,有方法加入() ,它是收集器 class的一部分。 You have to join this string using
Collectors.joining()
.您必须使用
Collectors.joining()
加入此字符串。
Here down is code:下面是代码:
String result = Stream.of("do", "re", "ma", "po", "fa", "si")
.filter(str -> str.length() > 1)
.collect(Collectors.joining());
Or或者
String result= Stream.of("do","re","ma","po","fa","si")
.filter(str -> str.length() > 1)
.peek(System.out::println)
.collect(Collectors.joining());
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: 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: Output:
do
re
ma
po
fa
si
doremapofasi
You misunderstand the mechanisms behind the Stream API .您误解了Stream API背后的机制。
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)).
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) ) 和终端操作(产生结果或副作用,例如 count() 或 forEach(Consumer))。
Streams are lazy
;Streams are lazy
;computation
on the source data isonly performed when the terminal operation is initiated
, and source elements are consumed only as needed.only performed when the terminal operation is initiated
对源数据进行computation
,并且仅在需要时消耗源元素。
The main conclusion:主要结论:
Operations on the data in the pipeline will be performed only if the terminal operation is initiated
.只有
if the terminal operation is initiated
才会对管道中的数据进行操作。
Stream without a terminal operation is perfectly valid from the compiler's perspective of view.从编译器的角度来看,没有终端操作的 Stream 是完全有效的。 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.您尝试打印的内容(
java.util.Spliterators$1Adapter@63961c42
)不是结果,而是 stream ZA8CFDE6331BD59EB26666F8911CB 本身。
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).要产生结果或副作用,流管道必须以终端操作结束(
collect()
、 reduce()
、 count()
、 forEach()
、 findFirst()
、 findAny
、 anyMatch()
- 在您的代码中注释掉)。 Note, that peek()
is an intermediate operation and confuse it with forEach()
.请注意,
peek()
是一个中间操作,并将其与forEach()
混淆。 You can use peek()
as many time as need, which can be useful for debugging purposes.您可以根据需要多次使用
peek()
,这对于调试目的很有用。
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 output
Single result: portum
Ignoranti
Ignoranti
petat
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. forEach
也是 stream 上的“终止”操作,这意味着它“实际执行”它。
you need to 'execute' the stream by calling collect or reduce您需要通过调用 collect 或 reduce 来“执行” stream
assertEquals("ab", Arrays.stream(new String[]{"a", "b"}).reduce("", (s,x) -> s + x));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.