连接不同的流并在 Java 上打印
Concat different Streams and print on Java
我正在尝试打印包含 String 和 Integer 元素的 concat Stream。我尝试了几种方法,但其中 none 行得通。我在互联网上找不到合适的方法。如您所见,这只是一个测试代码:
import java.util.stream.*;
class testconcat{
public static void main(String[] args){
Stream<String> part1=Stream.of("Testing the ");
Stream<String> part2=Stream.of("streams concat");
Stream<String> part3=Stream.of(" on Java, dividing ");
Stream<String> part4=Stream.of("this phrase in ");
IntStream part5=IntStream.of(6);
Stream<String> part6=Stream.of(" parts.");
String phrase=Stream.concat(Stream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4), part5), part6);
System.out.println(phrase);
}
}
我知道第 5 部分是一个整数,所以我不能以常规方式连接它,所以我也试过了:
IntStream part5=IntStream.of(6);
Stream<String> part6=Stream.of(" parts.");
String phrase=Stream.concat(IntStream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4), part5), part6);
System.out.println(phrase);
}
}
还有:
Integer part5=6;
String part6=(" parts.");
String phrase=Stream.concat(Stream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4);
System.out.println(phrase + Integer.toString(part5) + part6);
}
}
None 其中有效。有没有办法做到这一点?提前致谢。
如果我理解正确并且你想创建一个组合流字符串,你可以使用类似的东西:
Stream.concat(...):
Stream<Integer> integerStream = IntStream.range(72, 129).boxed(); // to convert to Stream<Integer>
Stream<String> stringStream =Stream.of(" String Stream");
String str = Stream.concat(integerStream.map(String::valueOf), stringStream).collect(Collectors.joining(","));
System.out.println(str);
或
Stream.of(...)
Stream<Integer> integerStream = IntStream.range(72, 129).boxed(); // to convert to Stream<Integer>
Stream<String> stringStream =Stream.of(" String Stream");
String str = Stream.of(integerStream.map(String::valueOf), stringStream).flatMap(t -> t).collect(Collectors.joining(","));
System.out.println(str);
I'm trying to print a concat Stream that includes String and Integer elements
错误,IntStream 是原始 int
值的特定流,它与 Stream 不同,因此必须调用 IntStream::boxed
或 IntStream::mapToObj
生成 Stream<?>
可用于 Stream::concat
:
Stream<?> many = Stream
.concat(part1, Stream
.concat(part2, Stream
.concat(part3, Stream
.concat(part4, Stream
.concat(part5.boxed(), part6)
)
)
)
);
To get the result of the string concatenation as String, a terminal operation `Stream.collect(Collectors.joining())` should be applied after mapping all the objects in the streams into String using `String::valueOf` (or `Objects::toString`):
```java
String result = many.map(String::valueOf).collect(Collectors.joining());
System.out.println(result);
// -> Testing the streams concat on Java, dividing this phrase in 6 parts.
但是,这种冗长的 Stream 连接是多余的,可以使用 Stream.of + Stream::flatMap
连接 Stream
。在下面的示例中,IntStream
立即映射到 String,因此不需要额外的 Stream::map
:
Stream<String> many = Stream
.of(part1, part2, part3, part4, part5.mapToObj(String::valueOf), part6)
.flatMap(s -> s);
String res = many.collect(Collectors.joining());
System.out.println(res);
// -> Testing the streams concat on Java, dividing this phrase in 6 parts.
我正在尝试打印包含 String 和 Integer 元素的 concat Stream。我尝试了几种方法,但其中 none 行得通。我在互联网上找不到合适的方法。如您所见,这只是一个测试代码:
import java.util.stream.*;
class testconcat{
public static void main(String[] args){
Stream<String> part1=Stream.of("Testing the ");
Stream<String> part2=Stream.of("streams concat");
Stream<String> part3=Stream.of(" on Java, dividing ");
Stream<String> part4=Stream.of("this phrase in ");
IntStream part5=IntStream.of(6);
Stream<String> part6=Stream.of(" parts.");
String phrase=Stream.concat(Stream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4), part5), part6);
System.out.println(phrase);
}
}
我知道第 5 部分是一个整数,所以我不能以常规方式连接它,所以我也试过了:
IntStream part5=IntStream.of(6);
Stream<String> part6=Stream.of(" parts.");
String phrase=Stream.concat(IntStream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4), part5), part6);
System.out.println(phrase);
}
}
还有:
Integer part5=6;
String part6=(" parts.");
String phrase=Stream.concat(Stream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4);
System.out.println(phrase + Integer.toString(part5) + part6);
}
}
None 其中有效。有没有办法做到这一点?提前致谢。
如果我理解正确并且你想创建一个组合流字符串,你可以使用类似的东西:
Stream.concat(...):
Stream<Integer> integerStream = IntStream.range(72, 129).boxed(); // to convert to Stream<Integer>
Stream<String> stringStream =Stream.of(" String Stream");
String str = Stream.concat(integerStream.map(String::valueOf), stringStream).collect(Collectors.joining(","));
System.out.println(str);
或
Stream.of(...)
Stream<Integer> integerStream = IntStream.range(72, 129).boxed(); // to convert to Stream<Integer>
Stream<String> stringStream =Stream.of(" String Stream");
String str = Stream.of(integerStream.map(String::valueOf), stringStream).flatMap(t -> t).collect(Collectors.joining(","));
System.out.println(str);
I'm trying to print a concat Stream that includes String and Integer elements
错误,IntStream 是原始 int
值的特定流,它与 Stream 不同,因此必须调用 IntStream::boxed
或 IntStream::mapToObj
生成 Stream<?>
可用于 Stream::concat
:
Stream<?> many = Stream
.concat(part1, Stream
.concat(part2, Stream
.concat(part3, Stream
.concat(part4, Stream
.concat(part5.boxed(), part6)
)
)
)
);
To get the result of the string concatenation as String, a terminal operation `Stream.collect(Collectors.joining())` should be applied after mapping all the objects in the streams into String using `String::valueOf` (or `Objects::toString`):
```java
String result = many.map(String::valueOf).collect(Collectors.joining());
System.out.println(result);
// -> Testing the streams concat on Java, dividing this phrase in 6 parts.
但是,这种冗长的 Stream 连接是多余的,可以使用 Stream.of + Stream::flatMap
连接 Stream
。在下面的示例中,IntStream
立即映射到 String,因此不需要额外的 Stream::map
:
Stream<String> many = Stream
.of(part1, part2, part3, part4, part5.mapToObj(String::valueOf), part6)
.flatMap(s -> s);
String res = many.collect(Collectors.joining());
System.out.println(res);
// -> Testing the streams concat on Java, dividing this phrase in 6 parts.