9

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.

List<Integer> i = new ArrayList<>();    //  i.add(null);
for (int j = 1; j < 6; j++) {
    i.add(j);
}
System.out.println(i.stream().collect(Collectors.joining(","))); // Line 8

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.

2
  • 2
    You need to map the Integers to Strings` to use joining. It returns Collector<CharSequence, ?, String> specifically.
    – Naman
    Aug 22, 2019 at 4:43
  • You can loop one more and give the value to string one by one with comma. Aug 22, 2019 at 4:57

4 Answers 4

45

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

System.out.println(i.stream().map(String::valueOf)
        .collect(Collectors.joining(",")));

Which outputs

1,2,3,4,5

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

System.out.println(
        IntStream.range(1, 6).boxed().map(String::valueOf)
               .collect(Collectors.joining(","))
);
7
  • The method description says "Returns a Collector that concatenates the input elements, separated by the specified delimiter, in encounter order." Shouldn't they mention input elements should only be String?
    – Nicky
    Aug 22, 2019 at 4:42
  • Also, i thought it will automatically calls toString method of Integer to solve this
    – Nicky
    Aug 22, 2019 at 4:43
  • Documentation also states: "Returns: A Collector which concatenates CharSequence elements, separated by the specified delimiter, in encounter order" (and return type is Collector<CharSequence,​?,​String>, that is, a Collector that accepts CharSequence)
    – user85421
    Aug 22, 2019 at 4:44
  • @Naman what if i don't use overloaded method. I can also use Collectors.joining() which says "Returns a Collector that concatenates the input elements into a String, in encounter order."
    – Nicky
    Aug 22, 2019 at 4:46
  • 1
    If you read the entire definition, it says static Collector<CharSequence,?,String> joining​(CharSequence delimiter) which tells you the type it works on (CharSequence). Aug 22, 2019 at 4:48
11

It is very easy with the Apache Commons Lang library.

Commons Lang

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
String str = org.apache.commons.lang.StringUtils.join(list, ","); // You can use any delimiter
System.out.println(str);  // Output: 1, 2, 3, 4, 5, 6, 7

Java 8 Solution

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
String joinedList = list.stream().map(String::valueOf).collect(Collectors.joining(","));
System.out.println(joinedList);
2

Using Guava's com.google.common.base.Joiner

String res = Joiner.on(",").join(integerlist);

Using the Java Stream API

String res = integerlist.stream()
                        .map(String::valueOf)
                        .collect(Collectors.joining(","));
0

Perhaps the answer to your question could be:

List.toString() as below:

List<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(3);

String listString = intList.toString();
System.out.println(listString); // <- this prints [1, 2, 3]

In this post below, it is very well explained. I hope you can find it useful:

Java: Convert List<Integer> to String

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.