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 do this, but I canβt say whether you agree to the additional verbosity and the creation of this line:
.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); }));
source share