Originally published on 2017-09-14
Java List interface doesn’t have a method to join all members to coma separated string.
Previous versions required iterating over the list and appending and instance of Stringbuilder.
Using Java 8 Streams makes it more neat.
https://gist.github.com/rmit-s3668804-Dzmitry-Kakaruk/7cb0f29c147cfb99e503f504ba3787f5
This file contains 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
// list of integer to string | |
List<Integer> numbers = Arrays.asList(1, 2, 3, 4); | |
String commaSeparatedNumbers = numbers.stream() | |
.map(i -> i.toString()) | |
.collect(Collectors.joining(", ")); |