This example shows how to convert String array to ArrayList in Java using Arrays, Collections, and Apache Commons including what is the most efficient way to convert.
How to convert String array to ArrayList in Java?
1) Convert Java String array to List using the Arrays class
Use the asList
method of the Arrays class to convert string array to a List object.
1 |
public static <T> List<T> asList(T… a) |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.javacodeexamples.stringexamples; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class StringArrayToArrayListExample { public static void main(String[] args){ String[] stringWeekdays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; List listDays = Arrays.asList(stringWeekdays); System.out.println("Convert String array to List"); for(String day : listDays){ System.out.println(day); } ArrayList aListDays = new ArrayList(listDays); } } |
Output
1 2 3 4 5 6 7 8 |
Convert String array to List Sun Mon Tue Wed Thu Fri Sat |
Please note that the List object returned by the asList
method is a fixed sized list which is backed by the original array. Adding or removing any elements from the list will throw java.lang.UnsupportedOperationException exception. However, you can update the values in the list.
1 2 |
//this will throw UnsupportedOperationException listDays.add("New Day"); |
Output
1 2 3 4 |
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.add(Unknown Source) at java.util.AbstractList.add(Unknown Source) at com.javacodeexamples.stringexamples.StringArrayToArrayListExample.main(StringArrayToArrayListExample.java:29) |
1 2 |
//this will also throw UnsupportedOperationException listDays.remove(0); |
Output
1 2 3 |
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(Unknown Source) at com.javacodeexamples.stringexamples.StringArrayToArrayListExample.main(StringArrayToArrayListExample.java:32) |
1 2 3 |
//change is permitted //Modify first element of list to "Sunday" instead of "Sun" listDays.set(0, "Sunday"); |
2) Using the Collections class
Use the addAll
method of the Collections class to add all elements of an array to the ArrayList.
1 2 3 4 5 6 7 8 9 10 11 12 |
String[] stringWeekdays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; ArrayList aListDays = new ArrayList(); Collections.addAll(aListDays, stringWeekdays); System.out.println("Convert String array to ArrayList example"); for(String day : aListDays){ System.out.println(day); } |
Output
1 2 3 4 5 6 7 8 |
Convert String array to ArrayList example Sun Mon Tue Wed Thu Fri Sat |
Please note that, if the ArrayList object had some elements before the Collections.addAll
method is called, all the elements of the String array will be added after existing elements of the ArrayList object.
3) Using Apache Commons Collections
If you are using the Apache Commons library, you can use the addAll
method of the CollectionUtils class to add all elements of a string array to an ArrayList as given below.
1 2 3 4 5 6 |
String[] stringWeekdays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; ArrayList aListDays = new ArrayList(); CollectionUtils.addAll(aListDays, stringWeekdays); |
What is the best way to convert String array to ArrayList?
If you do not intend to add or remove elements from the ArrayList after converting it from the string array, use the List object returned directly from the asList
method of the Arrays class. This is an efficient approach to convert string array to an ArrayList because it does not create a copy of an original array and operate on the same array.
If you want to add or remove elements from the ArrayList, convert that list to an ArrayList using the ArrayList constructor as given in the example code, or use the addAll
method of the Collections or CollectionUtils class.
This example is a part of the Java String tutorial and ArrayList in Java Tutorial.
Please let me know your views in the comments section below.