Is it possible to convert stream of chars str.chars()
to stream with Strings, where each String contains 5 characters, for example?
Asked
Active
Viewed 959 times
-1
-
8Yes. Everything is possible. Why not to try write some code for that by yourself? – Vlad DX Oct 28 '15 at 22:08
-
Tell me what method I should use for converting by some length, and I will try it – Letfar Oct 28 '15 at 22:11
-
2What does this have to do with `IntStream` - do you actually have an `IntStream` where the `int`s in the stream actually contain UTF-16 code points? – Jesper Oct 28 '15 at 22:16
-
I want to learn how to split text by Strings of 5 characters using stream. As I see `map()` is not suitable for this task. – Letfar Oct 28 '15 at 22:25
-
Here is how you can do it with 2 characters: http://stackoverflow.com/questions/20470010/collect-successive-pairs-from-a-stream which makes me think you should probably not use streams for that... – assylias Oct 28 '15 at 23:54
-
2@assylias, here's big difference: that question assumes overlapping pairs which is possible to implement for both sequential and parallel streams without external indexing. The feature requested here cannot be efficiently implemented for parallel stream as upon splitting you don't know in general case how many elements were actually skipped. So you have to choose either external indexing (as in WillShackleford answer) or poor parallel performance. – Tagir Valeev Oct 29 '15 at 01:15
3 Answers
6
You can simply split you string into five character sized strings using
String[] split = string.split("(?<=\\G.{5})");
If it has to be using streams, you may use, e.g.
Pattern.compile("(?<=\\G.{5})").splitAsStream(string).forEach(System.out::println);

Holger
- 285,553
- 42
- 434
- 765
-
2Tried to do the same, but failed. Did not know about `\G` feature, thanks. – Tagir Valeev Oct 29 '15 at 14:41
-
1
-
1It says “split when there were five characters since the last match”. `(?<=…)` is a “look-behind” clause to check what’s to the left of a potential split position, `\G` refers to the “last match” (or begin of string, initially), `.` means “any character” and `{5}` “five of them”. For more, see [the `Pattern` class](http://docs.oracle.com/javase/8/docs/api/?java/util/regex/Pattern.html) – Holger Nov 02 '15 at 09:28
3
I don't think trying to combine elements from the characters is a good fit for Java streams without using some third party libraries.
If you want a stream of 5 character substrings I would split them like this:
String s = "1234567890123456789012345678901234567890";
IntStream.range(0, s.length()/5)
.mapToObj(i -> s.substring(i*5, (i+1)*5))
.forEach(System.out::println);

WillShackleford
- 6,918
- 2
- 17
- 33
-
I am looking to acheive .range(0, length_of_each_string_in_list/2) how can I specify that? – Nirmal Apr 20 '18 at 04:01
1
Yes, it is possible if using a stateful lambda expression, but it is considered to be a bad practice.
One should be able to process the stream in serial or parallel. The order of the element processing would be different, but both should lead to the same result, which is possible only with stateless expressions.

user5500105
- 297
- 2
- 7