I have an ArrayList
that I want to output completely as a String. Essentially I want to output it in order using the toString
of each element separated by tabs. Is there any fast way to do this? You could loop through it (or remove each element) and concatenate it to a String but I think this will be very slow.
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.
If you were looking for a quick one-liner, as of Java 5 you can do this:
myList.toString().replaceAll("\\[|\\]", "").replaceAll(", ","\t")
Additionally, if your purpose is just to print out the contents and are less concerned about the "\t", you can simply do this:
myList.toString()
which returns a string like
[str1, str2, str3]
If you have an Array (not ArrayList) then you can accomplish the same like this:
Arrays.toString(myList).replaceAll("\\[|\\]", "").replaceAll(", ","\t")
I see quite a few examples which depend on additional resources, but it seems like this would be the simplest solution: (which is what I used in my own project) which is basically just converting from an ArrayList to an Array and then to a List.
List<Account> accounts = new ArrayList<>();
public String accountList()
{
Account[] listingArray = accounts.toArray(new Account[accounts.size()]);
String listingString = Arrays.toString(listingArray);
return listingString;
}
For seperating using tabs instead of using println you can use print
ArrayList<String> mylist = new ArrayList<String>();
mylist.add("C Programming");
mylist.add("Java");
mylist.add("C++");
mylist.add("Perl");
mylist.add("Python");
for (String each : mylist)
{
System.out.print(each);
System.out.print("\t");
}
How about this function:
public static String toString(final Collection<?> collection) {
final StringBuilder sb = new StringBuilder("{");
boolean isFirst = true;
for (final Object object : collection) {
if (!isFirst)
sb.append(',');
else
isFirst = false;
sb.append(object);
}
sb.append('}');
return sb.toString();
}
it works for any type of collection...
Changing List to a readable and meaningful String is really a common question that every one may encounter.
Case 1. If you have apache's StringUtils in your class path (as from rogerdpack and Ravi Wallau):
import org.apache.commons.lang3.StringUtils;
String str = StringUtils.join(myList);
Case 2 . If you only want to use ways from JDK(7):
import java.util.Arrays;
String str = Arrays.toString(myList.toArray());
Just never build wheels by yourself, dont use loop for this one-line task.
In Java 8 or later:
String listString = String.join(", ", list);
In case the list
is not of type String, a joining collector can be used:
String listString = list.stream().map(Object::toString)
.collect(Collectors.joining(", "));
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<String> list = new ArrayList<String>();
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 < list.size(); i++) {
sb.append(list.get(i));
if (i < list.size() - 1) {
sb.append("\t");
}
}
System.out.println(sb.toString());
Android has a TextUtil class you can use http://developer.android.com/reference/android/text/TextUtils.html
String implode = TextUtils.join("\t", list);
In one Line : From [12,0,1,78,12] to 12 0 1 78 12
String srt= list.toString().replaceAll("\\[|\\]|,","");
For this simple use case, you can simply join the strings with comma. If you use Java 8:
String csv = String.join("\t", yourArray);
otherwise commons-lang has a join() method:
String csv = org.apache.commons.lang3.StringUtils.join(yourArray, "\t");
List<String> stringList = getMyListOfStrings();
StringJoiner sj = new StringJoiner(" ");
stringList.stream().forEach(e -> sj.add(e));
String spaceSeparated = sj.toString()
You pass to the new StringJoiner
the char sequence you want to be used as separator. If you want to do a CSV: new StringJoiner(", ");
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
Would the following be any good:
List<String> streamValues = new ArrayList<>();
Arrays.deepToString(streamValues.toArray()));
You can use a Regex for this. This is as concise as it gets
System.out.println(yourArrayList.toString().replaceAll("\\[|\\]|[,][ ]","\t"));
This is quite an old conversation by now and apache commons are now using a StringBuilder internally: http://commons.apache.org/lang/api/src-html/org/apache/commons/lang/StringUtils.html#line.3045
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).
In case you happen to be on Android and you are not using Jack yet (e.g. because it's still lacking support for Instant Run), and if you want more control over formatting of the resulting string (e.g. you would like to use the newline character as the divider of elements), and happen to use/want to use the StreamSupport library (for using streams on Java 7 or earlier versions of the compiler), you could use something like this (I put this method in my ListUtils class):
public static <T> String asString(List<T> list) {
return StreamSupport.stream(list)
.map(Object::toString)
.collect(Collectors.joining("\n"));
}
And of course, make sure to implement toString() on your list objects' class.
An 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.
The below code may help you,
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
String str = list.toString();
System.out.println("Step-1 : " + str);
str = str.replaceAll("[\\[\\]]", "");
System.out.println("Step-2 : " + str);
Output:
Step-1 : [1, 2, 3]
Step-2 : 1, 2, 3
If you're using Eclipse Collections, you can use the makeString()
method.
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
Assert.assertEquals(
"one\ttwo\tthree",
ArrayListAdapter.adapt(list).makeString("\t"));
If you can convert your ArrayList
to a FastList
, you can get rid of the adapter.
Assert.assertEquals(
"one\ttwo\tthree",
FastList.newListWith("one", "two", "three").makeString("\t"));
Note: I am a committer for Eclipse Collections.
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<String> list = new ArrayList<String>();
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...
Download the Apache Commons Lang and use the method
StringUtils.join(list)
StringUtils.join(list, ", ") // 2nd param is the separator.
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 Apache Commons library and I also think it's a great addition to the Java Standard Library.
In Java 8 it's simple. See example for list of integers:
String result = Arrays.asList(1,2,3).stream().map(Object::toString).reduce((t, u) -> t + "\t" + u).orElse("");
Or multiline version (which is simpler to read):
String result = Arrays.asList(1,2,3).stream()
.map(Object::toString)
.reduce((t, u) -> t + "\t" + u)
.orElse("");
String result = Arrays.asList(1,2,3).stream()
.map(Object::toString)
.collect(Collectors.joining("\t"));
May not be the best way, but elegant way.
Arrays.deepToString(Arrays.asList("Test", "Test2")
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
System.out.println(Arrays.deepToString(Arrays.asList("Test", "Test2").toArray()));
}
}
Output
[Test, Test2]
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();
Source: Stackoverflow.com