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(", "));
Answer from Vitalii Fedorenko on Stack OverflowIn 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 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...
Videos
List<String> list = ..;
String[] array = list.toArray(new String[0]);
For example:
List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);
The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.
Important update: Originally the code above used new String[list.size()]. However, this blogpost reveals that due to JVM optimizations, using new String[0] is better now.
An alternative in Java 8:
String[] strings = list.stream().toArray(String[]::new);
Java 11+:
String[] strings = list.toArray(String[]::new);
Use like this.
List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");
String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);
for(String s : stockArr)
System.out.println(s);
Try this
String[] arr = list.toArray(new String[list.size()]);
First of all, this is more a use case for a map, where your code is the key and the string is the value. See http://docs.oracle.com/javase/7/docs/api/java/util/Map.html.
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "string1");
map.put(2, "string2");
map.get(1)
...
Second approach is to create a new object holding the code as an Integer and the String, like
public class StringHolder {
int code;
String string;
// constructor
// setter and getter
}
then use a for-each loop to iterate trough your array list and compare the code of the object with the given one.
Or, the rude, not recommended way: do some crazy string operations to figure out where your code is in the string. Acutally, do not do this.
If you must do it with an ArrayList I would suggest something like this:
public static void main(String[] args) {
List<String> yourData = new ArrayList<String>();
yourData.add("string1 string2 string3 1");
yourData.add("string4 string5 string6 2");
yourData.add("string7 string8 string9 3");
Iterator<String> dataIterator = yourData.iterator();
while(dataIterator.hasNext()) {
String d = dataIterator.next();
if(d.endsWith("2")) {
dataIterator.remove();
}
}
}
This assumes the code is a string, and is always the last word of your data.