Friday, June 23, 2017

3 Exampls to Convert an Array to ArrayList in Java

How to convert an Array to ArrayList in Java
Have you encountered any situation where you quickly wanted to convert your array to ArrayList or ArrayList to array in Java? I have faced many such situations that motivate me to write these quick Java tips about converting an array to ArrayList and ArrayList to array in Java. Both array and ArrayList are quite common and every Java developer is familiar with this. Former is used to store objects and primitive types while later can only hold objects. The array is part of standard Java fundamental data structure while ArrayList is part of the collection framework in Java.


This short array to ArrayList tutorial in Java will explain how quickly you can convert data from each other. So when you face such a situation don't bother just remember these tips and you will get through it. If you compare array vs ArrayList only significant difference is one is fixed size while other is not.

This article is in continuation of my post Difference between Vector and ArrayList in Java and how to Sort ArrayList in Java on descending order.

On related not from Java 5 onwards, ArrayList class supports Generics in Java, which means you can convert an ArrayList of String into a String array or ArrayList of Integer into an Integer Array. The generic provide type safety and remove casting during runtime.

Also, basic knowledge of essential data structure is also very important and that's why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course like Data Structures and Algorithms: Deep Dive Using Java on Udemy to fill the gaps in your understanding.

How to convert Array to ArrayList in Java

In first section of this Java tutorial, we will see how to convert from Array to ArrayList while in the second section we will see the opposite of it i.e. from ArrayList to Array. The main difference between these two is that once declared you can not change the size of the former while later is dynamic which re-sizes itself whenever its capacity crossed threshold specified by the load factor.

So you can say former is fixed-size and you later are re-sizable. java.util.Arrays class act as a bridge between array to ArrayList  and used to convert from array to ArrayList or ArrayList  to array. If you like to learn more about Array you may check How to detect if Array contains duplicate or not and How to sort Array elements in Java in ascending order.

Using Arrays.asList() method


List<String> assetList = Arrays.asList(asset); 


1) This method returns a List view of underlying array.

2) List returned by this method would be fixed size.






Array to ArrayList by using Collections.addAll method

convert array to arraylist in Java program
This example of converting an array to ArrayList is not that efficient as the earlier method but its more flexible.

List<String> assetList = new ArrayList();

Collections.addAll(assetList, asset);

Important point about this method of array to ArrayList conversion is :





Example of Converting ArrayList to Array using Collection's addAll method

This is another great way of converting an array to ArrayList. We are essentially using Collection interface's addAll() method for copying content from one list to another. Since List returned by Arrays.asList() is fixed-size it’s not much of use, this way we can convert that into proper ArrayList.

Arraylist newAssetList = new Arraylist();

These were the three methods to convert an array to ArrayList in Java. By the way, you can also create arrays of ArrayList because list can hold any type of object.

Array to List in Java using Spring Framework


System.out.println("Size of array: " + currency.length);
List<String> currencyList = CollectionUtils.arrayToList(currency);


How to convert ArrayList to Array in Java

Now this is a opposite side of story where you want to convert from Arraylist to Array, instead of array to ArrayList, interesting right. Just now we were looking at converting array to arraylist and now we need to back to former one. Anyway it’s simpler than you have probably thought again we have java.util.Arrays class as our rescue. This class acts as bridge between ArrayList to array.

Example of converting ArrayList to Array in Java

In this example of ArrayList to array we will convert our assetTradingList into String array.
ArrayList assetTradingList = new ArrayList(); 

assetTradingList.add("Stocks trading");
assetTradingList.add("futures and option trading");
assetTradingList.add("electronic trading");
assetTradingList.add("forex trading");
assetTradingList.add("gold trading");
assetTradingList.add("fixed income bond trading");
String [] assetTradingArray = new String[assetTradingList.size()];


Getting a clear idea of converting array to ArrayList and back to ArrayList and array saves a lot of time while writing code. It’s also useful if you like to initialize your ArrayList with some default data or want to add some elements into your existing ArrayList. these examples of converting array to ArrayList is by no means complete and please share your own ways of converting from ArrayList to array and vice-versa. If you are preparing for Java interview, don't forget to check my list of Top 25 Java Collection framework interview questions for experienced programmers.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Algorithms and Data Structures - Part 1 and 2
Data Structures in Java 9 by Heinz Kabutz

Other Java Collection tutorials you may like
Difference between ArrayList and HashSet in Java? (answer)
The difference between TreeMap and TreeSet in Java? (answer)
The difference between HashMap and ConcurrentHashMap in Java? (answer)
The difference between HashMap and LinkedHashMap in Java? (answer)
The difference between Hashtable and HashMap in Java? (answer)
The difference between HashSet and TreeSet in Java? (answer)
The difference between ArrayList and LinkedList in Java? (answer)
The difference between Vector and ArrayList in Java? (answer)
Difference between EnumMap and HashMap in Java



Anand Vijayakumar said...

Nice One Javin!!!



Muralidhar Nayani said...


ArrayList to HashSet

Anonymous said...


Unknown said...


In Example of converting ArrayList to Array in Java
i think this is declared wrongly..
the correct version is given below..

if i may wrong, please tell me

Anonymous said...

Both posts by Javin Paul:
and Vibin Arun:
are wrong. The correct one is:
Cheers. :)

Aakash Agarwal said...



is the correct one. Though I would say that

assetTradingArray = assetTradingList.toArray(assetTradingArray);

is much better.

Anonymous said...

The first method mentioned here is the best way to convert an array to ArrayList in Java because it is fast, doesn't take extra memory and just provide a List view over array. So if you just need a read only view of array as List, this is the one you should use, but if your intention is to modify the list or add, remove new elements then this will not work, because it's a fixed size ArrayList, it cannot grow dynamically.

In order to create dynamic ArrayList form array, simply use new ArrayList(Arrays.asList(array)). this will create a new object of ArrayList and copies element from array to list, which means you can change, add or remove new elements without affecting original array. The third way, which uses Collections.addAll() method is essentiall the same way, because it also copies content from array to list.

Post a Comment