Created
January 16, 2018 23:52
-
-
Save flaviut/660c47ba44e37a7ea937558cc178b771 to your computer and use it in GitHub Desktop.
provides a string joining implementation for Java < 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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