Suppose I have a text represented as a collection of lines of words. I want to join words in a line with a space, and join lines with a newline:
class Word {
String value;
}
public static String toString(List <List <Word>> lines) {
return lines.stream().map(
l -> l.stream().map(w -> w.value).collect(Collectors.joining(" "))
).collect(Collectors.joining("\n"));
}
This works fine, but I end up creating an intermediate String object for each line. Is there a nice concise way of doing the same without the overhead?
String s = List.of(
List.of(new Word("a"), new Word("b")),
List.of(new Word("c"), new Word("d")),
List.of(new Word("e"), new Word("f")))
.stream()
.collect(Collector.of(
() -> new StringJoiner(""),
(sj, list) -> {
list.forEach(x -> sj.add(x.getValue()).add(" "));
sj.add("\n");
},
StringJoiner::merge,
StringJoiner::toString));
EDIT
I can thing of this, but can't tell if you would agree for the extra verbosity vs creating that String:
.stream()
.collect(Collector.of(
() -> new StringJoiner(""),
(sj, list) -> {
int i;
for (i = 0; i < list.size() - 1; ++i) {
sj.add(list.get(i).getValue()).add(" ");
}
sj.add(list.get(i).getValue());
sj.add("\n");
},
StringJoiner::merge,
x -> {
String ss = x.toString();
return ss.substring(0, ss.length() - 1);
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With