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 .

3
  • 9
    I am looking for efficient solution -> First, do you know how to do it inefficiently? Commented Oct 21, 2012 at 20:19
  • @Roman: I think this is homework. Commented Oct 21, 2012 at 20:22
  • possible duplicate of Best way to convert an ArrayList to a string Commented Oct 22, 2012 at 20:32

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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. Well, why would you need an if for that? but that would be better to remove it forehand.
What's the point of using a StringBuilder if you still end up doing append("'" + eachElement + "', ")?
@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.
@BillJames. Ah! Man you're right. Sorry adarsh. will edit it. :)
|
0

Take a look at StringBuilder and StringBuffer:

StringBuffer

StringBuilder

Comments

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));
    }
}

Comments

0

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

    StringUtils.join(list, ", ");

or

String s = (!list.isEmpty())? "'" + StringUtils.join(list , "', '")+ "'":null;

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.