Skip to content

Instantly share code, notes, and snippets.

@yavuztas
Last active August 27, 2019 15:28
Show Gist options
  • Save yavuztas/aac21ba0aeafa5e51cfd04b10cec613b to your computer and use it in GitHub Desktop.
Save yavuztas/aac21ba0aeafa5e51cfd04b10cec613b to your computer and use it in GitHub Desktop.
Created with Copy to Gist
public static <T> String join(T[] array, String separator) {
return UtilsForCollections.join(array, separator, 0);
}
public static <T> String join(T[] array, String separator, int limit) {
if (limit > 0 && array.length > limit) {
return Arrays.stream(array)
.limit(limit)
.map(String::valueOf)
.collect(Collectors.joining(separator)) + separator + "...";
}
return Arrays.stream(array).map(String::valueOf)
.collect(Collectors.joining(separator));
}
public static <T> String join(Collection<T> collection, String separator) {
return UtilsForCollections.join(collection, separator, 0);
}
public static <T> String join(Collection<T> collection, String separator, int limit) {
if (limit > 0 && collection.size() > limit) {
return collection.stream()
.limit(limit)
.map(String::valueOf)
.collect(Collectors.joining(separator)) + separator + "...";
}
return collection.stream().map(String::valueOf)
.collect(Collectors.joining(separator));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment