guava icon indicating copy to clipboard operation
guava copied to clipboard

Provide Joiner as Collector

Open bjmi opened this issue 4 years ago • 1 comments

The main advantage of Guava Joiner over JDKs Collectors.joining(...) or StringJoiner that it can handle arbitrary objects and has a convenient builder api to express e.g. .useForNull(nullText).

Thus allow the usage of Joiner when streams come into play and get rid of cumbersome expressions like .filter(Objects::nonNull).map(Object::toString).

E.g. with factory method joiningOn(...):

objectStream.collect(joiningOn(", ").skipNulls());
objectStream.collect(joiningOn(", ").useForNull("<N/A>"));

Alternatively Joiner could implement Collector interface.

objectStream.collect(Joiner.on(", ").skipNulls());
objectStream.collect(Joiner.on(", ").useForNull("<N/A>"));

bjmi avatar Jul 03 '20 07:07 bjmi

There, I added a basic implementation. Not for the Android flavor, since it does not support Java 8 interfaces (if I read #5269 well).

Saucistophe avatar Oct 17 '20 23:10 Saucistophe