Java 8 stream operation
•
Java
Suppose I have a character stream named s Is it possible to convert a single string into a unary operation of two strings?
Therefore, if the original stream contains {a, B, C} and the operation converts each string s to s "1" and s "2", we will get: {A1, A2, B1, B2, C1, C2}
Is this possible (using lambda expressions)?
Solution
Yes, you can use flatmap or something
stream.flatMap(s -> Stream.of(s + "1",s + "2"));
Example:
Stream.of("a","b","c") // stream of "a","c" .flatMap(s -> Stream.of(s + "1",s + "2")) // stream of "a1","a2","b1","b2","c1","c2" .forEach(System.out::println);
Output:
a1 a2 b1 b2 c1 c2
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码