I'm wondering, how exactly will Joiner.join behave if it is given an empty collection:
Joiner.on(",").join(new ArrayList());
I wonder, what is the expected behavior here. Will it return an empty string or a null? Or it depends on the platform and implementation of the library?
Well, check source:
public String join(Iterable<? extends Entry<?, ?>> entries) {
return join(entries.iterator());
}
Next:
public String join(Iterator<? extends Entry<?, ?>> entries) {
return appendTo(new StringBuilder(), entries).toString();
}
And finally (skip one method):
public <A extends Appendable> A appendTo(A appendable, Iterator<? extends Entry<?, ?>> parts) throws IOException {
checkNotNull(appendable);
if (parts.hasNext()) {
...
}
return appendable;
}
If your collection is empty appendTo()
will return empty StringBuilder
. So, result is empty string.
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