Skip to content
Advertisement

Convert a list of integers into a comma-separated string

I was trying to convert a list of Integers into a string of comma-separated integers.

Collectors.joining(CharSequence delimiter) – Returns a Collector that concatenates the input elements, separated by the specified delimiter, in encounter order.

JavaScript

I am getting an error in line number 8:

The method collect(Collector<? super Integer,A,R>) in the type Stream is not applicable for the arguments (Collector<CharSequence,capture#20-of ?,String>)

Is there a way to do this by streams in Java 8?

If I create a list of strings with "1", "2", "3","4","5". it works.

Advertisement

Answer

Yes. However, there is no Collectors.joining for a Stream<Integer>; you need a Stream<String> so you should map before collecting. Something like,

JavaScript

Which outputs

JavaScript

Also, you could generate Stream<Integer> in a number of ways.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement