Best way to convert an ArrayList to a string?

Basically, using a loop to iterate over the ArrayList is the only option: ArrayList list = new ArrayList(); list. Add("one"); list. Add("two"); list.

Add("three"); String listString = ""; for (String s : list) { listString += s + "\t"; } System.out. Println(listString) In fact, a string concatenation is going to be just fine, as the javac compiler will optimize the string concatenation as a series of append operations on a StringBuilder anyway. Here's a part of the disassembly of the bytecode from the for loop from the above program: 61: new #13; //class java/lang/StringBuilder 64: dup 65: invokespecial #14; //Method java/lang/StringBuilder."":()V 68: aload_2 69: invokevirtual #15; //Method java/lang/StringBuilder.

Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 72: aload 4 74: invokevirtual #15; //Method java/lang/StringBuilder. Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 77: ldc #16; //String \t 79: invokevirtual #15; //Method java/lang/StringBuilder. Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 82: invokevirtual #17; //Method java/lang/StringBuilder.

ToString:()Ljava/lang/String As can be seen, the compiler optimizes that loop by using a StringBuilder so performance shouldn't be a big concern (OK, on second glance, the StringBuilder is being instantiated on each iteration of the loop, so it may not be the most efficient bytecode. Instantiating and using an explicit StringBuilder would probably yield better performance.) In fact, I think that having any sort of output (be it to disk or to the screen) will be at least an order of a magnitude slower than having to worry about the performance of string concatenations Edit: As pointed out in the comments, the above compiler optimization is indeed creating a new instance of StringBuilder on each iteration. (Which I have noted previously.

) The most optimized technique to use will be the response by Paul Tomblin as it only instantiates a single StringBuilder object outside of the for loop Rewriting to the above code to: ArrayList list = new ArrayList(); list. Add("one"); list. Add("two"); list.

Add("three"); StringBuilder sb = new StringBuilder(); for (String s : list) { sb. Append(s); sb. Append("\t"); } System.out.

Println(sb.toString()) Will only instantiate the StringBuilder once outside of the loop, and only make the two calls to the append method inside the loop, as evidenced in this bytecode (which shows the instantiation of StringBuilder and the loop): Instantiation of the StringBuilder outside loop: 33: new #8; //class java/lang/StringBuilder 36: dup 37: invokespecial #9; //Method java/lang/StringBuilder."":()V 40: astore_2 // snip a few lines for initializing the loop // Loading the StringBuilder inside the loop, then append: 66: aload_2 67: aload 4 69: invokevirtual #14; //Method java/lang/StringBuilder. Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 72: pop 73: aload_2 74: ldc #15; //String \t 76: invokevirtual #14; //Method java/lang/StringBuilder. Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 79: pop So, indeed the hand optimization should be better performing, as the inside of the for loop is shorter and there is no need to instantiate a StringBuilder on each iteration.

Basically, using a loop to iterate over the ArrayList is the only option: ArrayList list = new ArrayList(); list. Add("one"); list. Add("two"); list.

Add("three"); String listString = ""; for (String s : list) { listString += s + "\t"; } System.out. Println(listString); In fact, a string concatenation is going to be just fine, as the javac compiler will optimize the string concatenation as a series of append operations on a StringBuilder anyway. Here's a part of the disassembly of the bytecode from the for loop from the above program: 61: new #13; //class java/lang/StringBuilder 64: dup 65: invokespecial #14; //Method java/lang/StringBuilder."":()V 68: aload_2 69: invokevirtual #15; //Method java/lang/StringBuilder.

Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 72: aload 4 74: invokevirtual #15; //Method java/lang/StringBuilder. Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 77: ldc #16; //String \t 79: invokevirtual #15; //Method java/lang/StringBuilder. Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 82: invokevirtual #17; //Method java/lang/StringBuilder.

ToString:()Ljava/lang/String; As can be seen, the compiler optimizes that loop by using a StringBuilder, so performance shouldn't be a big concern. (OK, on second glance, the StringBuilder is being instantiated on each iteration of the loop, so it may not be the most efficient bytecode. Instantiating and using an explicit StringBuilder would probably yield better performance.

) In fact, I think that having any sort of output (be it to disk or to the screen) will be at least an order of a magnitude slower than having to worry about the performance of string concatenations. Edit: As pointed out in the comments, the above compiler optimization is indeed creating a new instance of StringBuilder on each iteration.(Which I have noted previously.) The most optimized technique to use will be the response by Paul Tomblin, as it only instantiates a single StringBuilder object outside of the for loop. Rewriting to the above code to: ArrayList list = new ArrayList(); list.

Add("one"); list. Add("two"); list. Add("three"); StringBuilder sb = new StringBuilder(); for (String s : list) { sb.

Append(s); sb. Append("\t"); } System.out. Println(sb.toString()); Will only instantiate the StringBuilder once outside of the loop, and only make the two calls to the append method inside the loop, as evidenced in this bytecode (which shows the instantiation of StringBuilder and the loop): // Instantiation of the StringBuilder outside loop: 33: new #8; //class java/lang/StringBuilder 36: dup 37: invokespecial #9; //Method java/lang/StringBuilder.

"":()V 40: astore_2 // snip a few lines for initializing the loop // Loading the StringBuilder inside the loop, then append: 66: aload_2 67: aload 4 69: invokevirtual #14; //Method java/lang/StringBuilder. Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 72: pop 73: aload_2 74: ldc #15; //String \t 76: invokevirtual #14; //Method java/lang/StringBuilder. Append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 79: pop So, indeed the hand optimization should be better performing, as the inside of the for loop is shorter and there is no need to instantiate a StringBuilder on each iteration.

Thanks for your answer. I didn't realize the compiler would probably optimize it! – Juan Besa Mar 1 '09 at 3:18 @Juan Besa: Although I've heard of this optimization in the past, this was the first time I actually tried to check if it really happens (by actually looking at the bytecode), so it was a learning opportunity for me as well :) – coobird Mar 1 '09 at 3:23 Consider using a StringBuilder class instead of just appending strings, for performance reasons.

– Jeremy Mar 1 '09 at 3:38 THe compiler will optimize the string concatenation but you will be creating a new StringBuilder object each time the loop is executed. – Pedro Henriques Mar 1 '09 at 3:43 I agree. I'd go with the StringBuilder too, for performance reasons.

– John Ellinwood Mar 1 '09 at 5:27.

Most Java projects often have apache-commons lang available. StringUtils.join() methods is very nice and has several flavors to meet almost every need. Public static java.lang.

String join(java.util. Collection collection, char separator) public static String join(Iterator iterator, String separator) { // handle null, zero and one elements before building a buffer Object first = iterator.next(); if (!iterator.hasNext()) { return ObjectUtils. ToString(first); } // two or more elements StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small if (first!

= null) { buf. Append(first); } while (iterator.hasNext()) { if (separator! = null) { buf.

Append(separator); } Object obj = iterator.next(); if (obj! = null) { buf. Append(obj); } } return buf.toString(); } Parameters: collection - the Collection of values to join together, may be null separator - the separator character to use Returns: the joined String, null if null iterator input Since: 2.3.

Loop through it and call toString. There isn't a magic way, and if there were, what do you think it would be doing under the covers other than looping through it? About the only micro-optimization would be to use StringBuilder instead of String, and even that isn't a huge win - concatenating strings turns into StringBuilder under the covers, but at least if you write it that way you can see what's going on.

StringBuilder out = new StringBuilder(); for (Object o : list) { out. Append(o.toString()); out. Append("\t"); } return out.toString().

Thanks! This is what I ending up doing :) – Juan Besa Mar 1 '09 at 5:50 1 For big arrays this is definitely not micro-optimizing. The automatic conversion to use StringBuilder is done separately for each string concatenation, which doesn't help in the case of a loop.It is OK if you concatenate a large number of elements in one expression.

– starblue Mar 1 '09 at 10:43.

Download the Jakarta Commons Lang and use the method StringUtils. Join(...). You can implement it by yourself, of course, but their code is fully tested and is probably the best possible implementation.

I am a big fan of the Jakarta Commons library and I also think it's a great addition to the Java Standard Library.

2 Unfortunately, the denizens of SO prefer to reinvent the wheel. – skaffman Jul 29 '09 at 7:14 1 Also in Apache Commons Lang. Usage would look like StringUtils.

Join(list.toArray(),"\t") – Muhd Mar 24 at 5:14.

This is a pretty old question, but I figure I might as well add a more modern answer - use the Joiner class from Guava: String joined = Joiner. On('\t'). Join(list).

If each element has a non-trivial string representation, and you want tabs inserted, the only way to do this is by looping.

It's an O(n) algorithm either way (unless you did some multi-threaded solution where you broke the list into multiple sublists, but I don't think that is what you are asking for). Just use a StringBuilder as below: StringBuilder sb = new StringBuilder(); for (Object obj : list) { sb. Append(obj.toString()); sb.

Append("\t"); } String finalString = sb.toString(); The StringBuilder will be a lot faster than string concatenation because you won't be re-instantiating a String object on each concatenation.

The most elegant way to deal with trailing separation characters is to use Class Separator StringBuilder buf = new StringBuilder(); Separator sep = new Separator("\t"); for (String each: list) buf. Append(sep). Append(each); String s = buf.toString(); The toString method of Class Separator returns the separater, except for the first call.

Thus we print the list without trailing (or in this case) leading separators.

If you don't want the last \t after the last element, you have to use the index to check, but remember that this only "works" (i.e. Is O(n)) when lists implements the RandomAccess. List list = new ArrayList(); list.

Add("one"); list. Add("two"); list. Add("three"); StringBuilder sb = new StringBuilder(list.size() * apprAvg); // every apprAvg > 1 is better than none for (int I = 0; I Append(list.

Get(i)); if (i Println(sb.toString()).

This is quite an old conversation by now and apache commons are now using a StringBuilder internally: commons.apache.org/lang/api/src-html/org... This will as we know improve performance, but if performance is critical then the method used might be somewhat inefficient. Whereas the interface is flexible and will allow for consistent behaviour across different Collection types it is somewhat inefficient for Lists, which is the type of Collection in the original question. I base this in that we are incurring some overhead which we would avoid by simply iterating through the elements in a traditional for loop.

Instead there are some additional things happening behind the scenes checking for concurrent modifications, method calls etc. The enhanced for loop will on the other hand result in the same overhead since the iterator is used on the Iterable object (the List).

If you happen to be doing this on Android, there is a nice utility for this called TextUtils which has a . Join(String delimiter, Iterable) method. List list = new ArrayList(); list.

Add("Item 1"); list. Add("Item 2"); String joined = TextUtils. Join(", ", list); Obviously not much use outside of Android, but figured I'd add it to this thread...

Quick and dirty one liner, "almost" right: ArrayList list; String s = list.toString(). Replace(",","\t").

4 only correct if you can guarantee that each toString() of the element in the array does not produce a comma. – Chii Mar 1 '09 at 13:15 Wrong assumption about comma – Ravi Gupta Jan 5 '10 at 5:38 3 What part of "quick and dirty" and "almost" is unclear? – Steve B.

Jan 29 '10 at 14:33.

In fact, a string concatenation is going to be just fine, as the javac compiler will optimize the string concatenation as a series of append operations on a StringBuilder anyway. As can be seen, the compiler optimizes that loop by using a StringBuilder, so performance shouldn't be a big concern. (OK, on second glance, the StringBuilder is being instantiated on each iteration of the loop, so it may not be the most efficient bytecode.

In fact, I think that having any sort of output (be it to disk or to the screen) will be at least an order of a magnitude slower than having to worry about the performance of string concatenations. Edit: As pointed out in the comments, the above compiler optimization is indeed creating a new instance of StringBuilder on each iteration. The most optimized technique to use will be the response by Paul Tomblin, as it only instantiates a single StringBuilder object outside of the for loop.

So, indeed the hand optimization should be better performing, as the inside of the for loop is shorter and there is no need to instantiate a StringBuilder on each iteration.

This is about as simple as it gets: StringBuilder result = new StringBuilder(chars.size()); for (Character c : chars) { result. Append(c); } String output = result.toString().

2 Might want to add the list's length as the initial capacity in StringBulder's constructor. That way it won't need to internally grow an array. For the same reason adarshr's approach below might be faster.

If speed is a concern I would benchmark both approaches. – Peter Abeles 9 hours ago Good idea, I'll add that. – Sean Owen 8 hours ago.

Yes, unfortunately, you'll have to iterate. Char cs = new charchars.size(); for(int I = 0; I Get(i); } String output = new String(cs).

String text = Arrays. ToString(chars. ToArray(new Characterchars.size())).

Replace("", ""). Replace("", ""). Replace(", ", ""); Though this seems to be inefficient way, but at least it makes it without explicitly iterating.

Relying on toString for doing anything but debugging / logging is a terrible idea – adarshr 5 hours ago.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions