Skip to content

Instantly share code, notes, and snippets.

@flaviut
Created January 16, 2018 23:52
Show Gist options
  • Save flaviut/660c47ba44e37a7ea937558cc178b771 to your computer and use it in GitHub Desktop.
Save flaviut/660c47ba44e37a7ea937558cc178b771 to your computer and use it in GitHub Desktop.
provides a string joining implementation for Java < 8
/**
* Joins the collection with string 'delimiter'. Somehow this function
* was overlooked in the stdlib until Java 8.
* <p>
* delimiter and collection must not be null.
*/
private static <T> String join(String delimiter, Collection<T> collection) {
// PRECONDITION delimiter and collection must not be null
StringBuilder result = new StringBuilder();
Iterator<T> iter = collection.iterator();
while (iter.hasNext()) {
result.append(iter.next());
if (iter.hasNext())
result.append(delimiter);
}
return result.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment