Assume a list:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Using String.join() with map()
String result = numbers.stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
System.out.println(result);
Output:
1,2,3,4,5
Summary:
map(String::valueOf)converts integers to strings.Collectors.joining(",")concatenates them with commas.- Works for any list of numeric or object values with a proper
toString()implementation.
