There may be a scenario when you want to convert an array to an ArrayList in Java in order to use some of the methods provided by ArrayList in Java. As example using contains() method provided by List to check if an element is there in the list.
Some of options you have to convert Array to ArrayList in Java are as follows.
- Arrays.asList(T... a) method that returns a fixed-size list backed by the specified array. One problem with using this option is that the returned list is static and trying to modify it will result in UnsupportedOperationException. See example.
- Using Collections.addAll(Collection<? super T> c, T... elements) where elements to be added to the collection can be passed as an array. The List you get by using this method is independent of the array making it a better choice for conversion of array to ArrayList. See example.
- Another option to convert array to ArrayLIst is using the collect method of the Java Stream API from Java 8 onward. See example.
Converting array to ArrayList using Arrays.asList() method
Arrays class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static
factory that allows arrays to be viewed as lists. The method to convert Array to ArrayList is asList()
whose
general form is
public static <T> List<T> asList(T... a)
In the following Java code there is an array of cities which is converted to an ArrayList.
public class CovertToArrayList { public static void main(String[] args) { String cityArray[] = {"Delhi", "Mumbai", "Bangalore", "Hyderabad", "Chennai"}; //Converting array to List List<String> cityList = Arrays.asList(cityArray); for(String name : cityList){ System.out.println("City : " + name); } } }
There are few points to note about Arrays.asList()
method
- This method returns a List that is a view onto the array. You can say it is more of a wrapper around an array. That makes it more efficient than any other option as array elements are not actually copied to the List.
- The list returned by this method is a fixed-size list.
- Since list returned is just a view onto the array, if you change any element in the list that change is reflected in the array too.
- The ArrayList class used to create the list while using this method Arrays.asList is actually a static class within the Arrays class and many of the methods of the original ArrayList are not supported by this ArrayList. As example add() method is not there in this ArrayList so any attempt to add new element to the returned list will throw UnsupportedOperationException. Following example shows the same.
public class CovertToArrayList { public static void main(String[] args) { String cityArray[] = {"Delhi", "Mumbai", "Bangalore", "Hyderabad", "Chennai"}; //Converting array to List List<String> cityList = Arrays.asList(cityArray); // Attempt to add new element in the list // This will throw exception cityList.add("Pune"); for(String name : cityList){ System.out.println("City : " + name); } } }
Output
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.add(Unknown Source) at java.util.AbstractList.add(Unknown Source) at org.netjs.prog.CovertToArrayList.main(CovertToArrayList.java:16)
List<String> cityList = Arrays.asList("Delhi", "Mumbai", "Bangalore", "Hyderabad", "Chennai");
As stated above no new element can be added to the List returned by Arrays.asList method. If you have to add new elements to the List returned using asList method then you need to wrap the list returned by Arrays.asList to a new ArrayList after conversion of array to ArrayList.
Wrapping the list returned by Arrays.asList to a new ArrayList
public class CovertToArrayList { public static void main(String[] args) { String cityArray[] = {"Delhi", "Mumbai", "Bangalore", "Hyderabad", "Chennai"}; //Converting array to List ArrayList<String> cityList = new ArrayList<String>( Arrays.asList(cityArray)); cityList.add("Pune"); for(String name : cityList){ System.out.println("City : " + name); } } }
Output
City : Delhi City : Mumbai City : Bangalore City : Hyderabad City : Chennai City : Pune
It can be seen that now new elements can be added to the list.
Converting Array to ArrayList in Java using Collections.addAll method
In this option to convert array to ArrayList using Collections.addAll()
method both array and ArrayList are passed as parameters to this method and the elements of array are added to the passed ArrayList. ArrayList you get from this method is independent from the array and can be modified without the change reflecting onto array and with out throwing any exception.
public class CovertToArrayList { public static void main(String[] args) { String cityArray[] = {"Delhi", "Mumbai", "Bangalore", "Hyderabad", "Chennai"}; List<String> cityList= new ArrayList<String>(); Collections.addAll(cityList, cityArray); cityList.add("Pune"); for(String name : cityList){ System.out.println("City : " + name); } } }
Convert Array to ArrayList using Stream API collect() method
Stream API in Java gives you another option to convert Array to ArrayList by using collect()
method.
public class CovertToArrayList { public static void main(String[] args) { String cityArray[] = {"Delhi", "Mumbai", "Bangalore", "Hyderabad", "Chennai"}; // Using stream to convert array to ArrayList List<String> cityList = Stream.of(cityArray).collect(Collectors.toList()); cityList.add("Pune"); for(String name : cityList){ System.out.println("City : " + name); } } }
Output
City : Delhi City : Mumbai City : Bangalore City : Hyderabad City : Chennai City : Pune
That's all for this topic How to Convert Array to ArrayList in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
One of the few good websites that teaches good java. Keep posting. Thanks!
ReplyDeleteThanks for the good words and so glad that you like it!
Delete