4

I have an ArrayList and I need to convert it to one String.

Each value in the String will be inside mark and will be separated by comma something like this:

ArrayList list = [a,b,c]

String s = " ’a’,’b’,’c’ ";

I am looking for efficient solution .

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
angus
  • 3,210
  • 10
  • 41
  • 71
  • 9
    `I am looking for efficient solution ` -> First, do you know how to do it inefficiently? – Rohit Jain Oct 21 '12 at 20:19
  • @Roman: I think this is homework. – nkr Oct 21 '12 at 20:22
  • possible duplicate of [Best way to convert an ArrayList to a string](http://stackoverflow.com/questions/599161/best-way-to-convert-an-arraylist-to-a-string) – Kai Oct 22 '12 at 20:32

4 Answers4

8

You can follow these steps: -

  • Create an empty StringBuilder instance

    StringBuilder builder = new StringBuilder();
    
  • Iterate over your list

  • For each element, append the representation of each element to your StringBuilder instance

    builder.append("'").append(eachElement).append("', ");
    
  • Now, since there would be a last comma left, you need to remove that. You can use StringBuilder.replace() to remove the last character.

You can take a look at documentation of StringBuilder to know more about various methods you can use.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • I really like your idea of getting rid of the one comma too many. I always do it with an if inside the loop, but your solution is more efficient. – Zane Oct 21 '12 at 20:28
  • @Zane. Well, why would you need an `if` for that? but that would be better to remove it forehand. – Rohit Jain Oct 21 '12 at 20:30
  • What's the point of using a `StringBuilder` if you still end up doing `append("'" + eachElement + "', ")`? – adarshr Oct 21 '12 at 20:40
  • @adarshr. Isn't StringBuilder there for that purpose only?? To modify the existing string content? – Rohit Jain Oct 21 '12 at 20:48
  • 1
    @Rohit Adarshr's complaint is that the line should be replaced with 3 appends... since you're doing String addition in the append, which is what you try to avoid by using a StringBuilder in the first place. – billjamesdev Oct 21 '12 at 20:50
  • 1
    @BillJames. Ah! Man you're right. Sorry adarsh. will edit it. :) – Rohit Jain Oct 21 '12 at 20:52
0

Take a look at StringBuilder and StringBuffer:

StringBuffer

StringBuilder

Makoto
  • 104,088
  • 27
  • 192
  • 230
Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
0

Maybe an overkill here but providing a more functional approach through Guava:

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Collections2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


public class Main {

    public static void main(String ... args){
        List<String> list = new ArrayList(){{add("a");add("b");add("c");}};
        Collection<String> quotedList = Collections2.transform(list,new Function<String, String>() {
            @Override
            public String apply(String s) {
                return "'"+s+"'";
            }
        });
        System.out.println(Joiner.on(",").join(quotedList));
    }
}
dimitrisli
  • 20,895
  • 12
  • 59
  • 63
0

use StringUtils library from Apache org.apache.commons.lang3.StringUtils;

    StringUtils.join(list, ", ");

or

String s = (!list.isEmpty())? "'" + StringUtils.join(list , "', '")+ "'":null;
Marcin Wasiluk
  • 4,675
  • 3
  • 37
  • 45