Split string in a stream Java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Split string in a stream Java



I have a POJO class Product


List<Product> list = new ArrayList<>();
list.add(new Product(1, "HP Laptop Speakers", 25000));
list.add(new Product(30, "Acer Keyboard", 300));
list.add(new Product(2, "Dell Mouse", 150));



Now I want to split the list to get output as HP-Laptop-Speakers&&Acer-Keyboard&&Dell-Mouse.


HP-Laptop-Speakers&&Acer-Keyboard&&Dell-Mouse.



I just want a single liner in stream.
So far I have managed to get


Optional<String> temp = list.stream().
map(x -> x.name).
map(x -> x.split(" ")[0]).
reduce((str1, str2) -> str1 + "&&" + str2);
System.out.println(temp.get());



Output: HP&&Acer&&Dell


HP&&Acer&&Dell



Can someone help me out.
Thanks in advance.





You want to join, not split. Join = take several parts and construct a single string. Split = take a single string and extract several parts.
– JB Nizet
Aug 8 at 18:30




6 Answers
6



First, the split() operation is not necessary. While you could split all the pieces and then join them together like that, it is far simpler to use a replace or replaceAll call instead.


split()


replace


replaceAll



Secondly, the reduce operation will not be very efficient since it is creating lots of intermediary Strings and StringBuilders. Instead, you should use the String joining Collector, which is more efficient:


String


StringBuilder


String


String temp = list.stream()
.map(x -> x.name.replace(" ", "-"))
.collect(Collectors.joining("&&"));



The second map operation you write keep the first word.


map



Instead, here what you can do :



Replace the spaces (\s as regex) by a -


\s


-


Optional<String> temp = list.stream()
.map(x -> x.name)
.map(x -> x.replaceAll("\s", "-"))
.reduce((str1, str2) -> str1 + "&&" + str2);



Split on space, and then join with the -


-


Optional<String> temp = list.stream()
.map(x -> x.name)
.map(x -> String.join("-", x.split("\s")))
.reduce((str1, str2) -> str1 + "&&" + str2);



Try to use collector on stream of strings:


.collect(Collectors.joining("&&"))



One easy way to do this would be to override the toString() method in the Product class to print the name as you want it formatted and then iterate through the list concatenating each result onto a longer string.


Optional<String> temp = list.stream().map(x -> x.name)
.map(x -> x.replaceAll("\s", "-"))
.reduce((str1, str2) -> str1 + "&&" + str2);



you need to replace this portion map(x -> x.split(" ")) with map(x -> x.replaceAll("\s","-"))


map(x -> x.split(" "))


map(x -> x.replaceAll("\s","-"))






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard