72

How can I convert List<Integer> to String? E.g. If my List<Integer> contains numbers 1 2 and 3 how can it be converted to String = "1,2,3"? Every help will be appreciated.

9
  • 4
    Why the downvotes? It's a good question and neither @GermannArlington or orangegoat have answered it. Commented Nov 2, 2012 at 15:58
  • 1
    Probably because "the question does not show any research effort". Commented Nov 2, 2012 at 15:59
  • 5
    Still, it's a useful question and from the answer it's not obvious. I think it's quite obvious that if a user is asking a question like this they are very new to the language and therefore this is a reasonable question. If there are no duplicate questions (I did't check) then that's an even stronger case. IMHO. Commented Nov 2, 2012 at 16:04
  • 1
    I've tried to find solution but I could't find solution that solves my problem. I don't understand what is wrong with my question... Commented Nov 2, 2012 at 16:08
  • 4
    @Martin, Joe: when you ask such a question, don't just ask for a solution for your problem. Show what you have tried to solve the problem, and explain what you're having problems with. This is what consitutes a "Research effort". Otherwise, it just looks like: "Hey, I'm too lazy to find out how to do this simple thing. Could anyone do it for me?". Commented Nov 2, 2012 at 16:11

5 Answers 5

59

In vanilla Java 8 (streams) you can do

// Given numberList is a List<Integer> of 1,2,3...

String numberString = numberList.stream().map(String::valueOf)
    .collect(Collectors.joining(","));

// numberString here is "1,2,3"
Sign up to request clarification or add additional context in comments.

1 Comment

Better, nicer, newer way to do it properly ++1!
53

I think you may use simply 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]

If you don't want [] in the string, simply use the substring e.g.:

   listString = listString.substring(1, listString.length()-1); 
   System.out.println(listString); //<- this prints 1, 2, 3

Please note: List.toString() uses AbstractCollection#toString method, which converts the list into String as above

2 Comments

The format of the string returned by AbstractCollection#toString is implementation specific I assume?
This is a bit hacky and outdated imo.
52

With Guava:

String s = Joiner.on(',').join(integerList);

2 Comments

Downvoter: why the downvote? Don't you find the above much more elegant and readable than the other options?
I think this answer is valid for the question. OP didn't explicitly specify that he/she don't want to use third party libraries. I will compensate with +1.
11

One way would be:

Iterate over list, add each item to StringBuffer (or) StringBuilder and do toString() at end.

Example:

StringBuilder strbul  = new StringBuilder();
     Iterator<Integer> iter = list.iterator();
     while(iter.hasNext())
     {
         strbul.append(iter.next());
        if(iter.hasNext()){
         strbul.append(",");
        }
     }
 strbul.toString();

2 Comments

StringBuilder if using Java 5 or greater
list.stream().map(String::valueOf).collect(Collectors.joining(",")) since Java 8.
8

Just to add another (of many) options from a popular library (Apache Commons):

import org.apache.commons.lang3.StringUtils;

String joinedList = StringUtils.join(someList, ",");

See documentation: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#join-java.lang.Iterable-java.lang.String-


An elegant option from others' comments (as of Java 8):

String joinedList = someList.stream().map(String::valueOf).collect(Collectors.joining(","));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.