How do I convert an array to an ArrayList in Java? I have the following array of Element objects:
Element[] array = {new Element(1), new Element(2), new Element(3)};
And I want to convert it to an ArrayList of Element type:
ArrayList<Element> arrayList = ...;
What is the proper way to perform this conversion in Java?
To convert an array to an ArrayList in Java, the most proper way is to use new ArrayList<>(Arrays.asList(array)), which creates a true ArrayList that can be modified dynamically rather than the fixed-size list that Arrays.asList() alone returns. This approach ensures you get a standard ArrayList with all its dynamic resizing capabilities while efficiently copying the array elements.
Contents
- Understanding Array to ArrayList Conversion
- Best Practices for Conversion
- Performance Considerations
- Common Pitfalls and Solutions
- Java 8 and Later Alternatives
Understanding Array to ArrayList Conversion
When working with Java collections, converting arrays to ArrayLists is a common requirement for dynamic operations. The Arrays.asList() method is the foundation for most conversion approaches, but understanding its behavior is crucial for proper implementation.
The Arrays.asList() Method
The Arrays.asList() method returns a fixed-size list backed by the specified array [source]. This means:
- The list maintains a direct reference to your original array
- No elements are physically copied to a new collection
- The returned list has a fixed size and cannot grow or shrink
Element[] array = {new Element(1), new Element(2), new Element(3)};
List<Element> list = Arrays.asList(array); // Fixed-size list
Key Difference: Arrays.ArrayList vs java.util.ArrayList
It’s important to recognize that Arrays.asList() returns a java.util.Arrays.ArrayList, which is different from java.util.ArrayList [source]. The Arrays.ArrayList is a private static class inside the Arrays class that provides a list view over an array but lacks the dynamic capabilities of a standard ArrayList.
Best Practices for Conversion
Method 1: Standard ArrayList Construction
The recommended approach for creating a modifiable ArrayList is:
Element[] array = {new Element(1), new Element(2), new Element(3)};
ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array));
Why this is best:
- Creates a true
java.util.ArrayListwith dynamic resizing capabilities - Allows adding, removing, and modifying elements freely
- Maintains all ArrayList methods and functionality
- Efficiently copies the array elements to the new collection
Method 2: Manual Loop Approach
For scenarios requiring more control, you can manually convert the array:
Element[] array = {new Element(1), new Element(2), new Element(3)};
ArrayList<Element> arrayList = new ArrayList<>();
for (Element element : array) {
arrayList.add(element);
}
Advantages:
- Provides full control over the conversion process
- Allows filtering or transformation during conversion
- Works with primitive arrays (though requires additional steps)
Performance Considerations
Memory Efficiency
The new ArrayList<>(Arrays.asList(array)) approach is memory efficient because it doesn’t create unnecessary intermediate objects [source]. The constructor efficiently copies the array elements into the new ArrayList’s internal array.
Performance Comparison
- Arrays.asList(): O(1) time complexity - just creates a wrapper around the existing array
- new ArrayList<>(Arrays.asList()): O(n) time complexity - copies all elements to a new backing array
- Manual loop: O(n) time complexity - similar to the constructor approach
For most use cases, the performance difference is negligible unless dealing with extremely large arrays [source].
When to Use Each Approach
| Approach | Use Case | Performance | Modifiable |
|---|---|---|---|
Arrays.asList() |
When you need a read-only view | Best (O(1)) | No |
new ArrayList<>(Arrays.asList()) |
When you need a dynamic ArrayList | Good (O(n)) | Yes |
| Manual loop | When you need element transformation | Good (O(n)) | Yes |
Common Pitfalls and Solutions
Pitfall 1: UnsupportedOperationException
A common mistake is trying to modify the result of Arrays.asList() directly:
// INCORRECT - will throw UnsupportedOperationException
List<Element> list = Arrays.asList(array);
list.add(new Element(4)); // Throws exception!
Solution: Always wrap the result in a new ArrayList when modifications are needed:
// CORRECT
List<Element> list = new ArrayList<>(Arrays.asList(array));
list.add(new Element(4)); // Works fine
Pitfall 2: Unintended Side Effects
Because Arrays.asList() returns a list backed by the original array, changes to either affect each other:
Element[] array = {new Element(1), new Element(2), new Element(3)};
List<Element> list = Arrays.asList(array);
list.set(0, new Element(99)); // Modifies the original array!
System.out.println(array[0].getValue()); // Output: 99
Solution: Use new ArrayList<>(Arrays.asList(array)) to create an independent copy.
Pitfall 3: Primitive Arrays
When working with primitive arrays (int[], double[], etc.), Arrays.asList() doesn’t work as expected:
int[] primitiveArray = {1, 2, 3};
List<Integer> list = Arrays.asList(primitiveArray); // Returns List<int[]>, not List<Integer>
Solution: Use Java 8 Streams for primitive arrays:
int[] primitiveArray = {1, 2, 3};
List<Integer> list = Arrays.stream(primitiveArray)
.boxed()
.collect(Collectors.toList());
Java 8 and Later Alternatives
Stream API Approach
Java 8 introduced Streams, providing another elegant way to convert arrays to ArrayLists:
Element[] array = {new Element(1), new Element(2), new Element(3)};
ArrayList<Element> arrayList = Arrays.stream(array)
.collect(Collectors.toCollection(ArrayList::new));
Benefits of Stream Approach
- More flexible and expressive
- Allows easy filtering and transformation during conversion
- Consistent with other Java 8+ collection operations
- Better support for parallel processing
List.of() (Java 9+)
For creating unmodifiable lists, Java 9+ provides List.of():
// Creates an unmodifiable list
List<Element> list = List.of(array);
Note: List.of() creates an unmodifiable list, so it’s not suitable when you need to modify the collection.
Conclusion
Key Takeaways
- Always use
new ArrayList<>(Arrays.asList(array))when you need a modifiable ArrayList from an array - Avoid direct use of
Arrays.asList()when you need to add, remove, or resize the collection - Understand the difference between
Arrays.ArrayListandjava.util.ArrayList - Consider Java 8 Streams for more complex conversion scenarios or primitive arrays
Practical Recommendations
- For simple, dynamic ArrayList conversion:
new ArrayList<>(Arrays.asList(array)) - For element transformation during conversion: Manual loop or Stream API
- For read-only operations:
Arrays.asList()orList.of()(Java 9+) - For primitive arrays: Use
Arrays.stream().boxed().collect()
By following these best practices, you’ll avoid common pitfalls and ensure efficient, reliable array-to-ArrayList conversions in your Java applications.
Sources
- Arrays.asList vs new ArrayList(Arrays.asList()) | Baeldung
- Java Arrays.asList() vs new ArrayList() - HowToDoInJava
- java - Difference between Arrays.asList(array) and new ArrayList(Arrays.asList(array)) - Stack Overflow
- Array to ArrayList Conversion in Java - GeeksforGeeks
- Converting Between an Array and a List in Java - Baeldung
- How to convert array to ArrayList in Java - LabEx
- Convert Array to ArrayList - JavaGuides
- 3 Ways to Convert an Array to ArrayList in Java - Java Revisited