Logo

How do I convert an array to a list in Java?

Converting an array into a list in Java is a very common operation—especially when you want to leverage list-specific operations like searching, inserting, or removing elements. Below are several practical ways to make this conversion, along with nuances you should be aware of.

1. Using Arrays.asList(...)

String[] array = {"apple", "banana", "cherry"}; List<String> list = Arrays.asList(array); System.out.println(list); // Output: [apple, banana, cherry]
  • Key Details:
    1. The returned list is fixed-size. You can’t add or remove elements directly from it (though you can modify the existing elements).
    2. Any changes you make to the array reflect in the list, and vice versa, because they share the same underlying data.

2. Creating a New ArrayList from Arrays.asList(...)

If you need a truly mutable list, you can wrap the result of Arrays.asList(...) in a new ArrayList:

String[] array = {"apple", "banana", "cherry"}; List<String> list = new ArrayList<>(Arrays.asList(array)); list.add("date"); System.out.println(list); // Possible Output: [apple, banana, cherry, date]
  • Advantages:
    • You can freely add or remove elements.
    • Changes to the underlying array won’t affect this list, and vice versa.

3. Using a Loop (For Custom Logic)

If you need more control—such as filtering or transforming elements—you can manually iterate over the array and populate a new list:

String[] array = {"apple", "banana", "cherry"}; List<String> list = new ArrayList<>(); for (String fruit : array) { // Example: Only add fruits with >5 letters if (fruit.length() > 5) { list.add(fruit); } } System.out.println(list);
  • Use Case: Perfect if you need some custom processing before populating the list.

Performance Considerations

  1. Arrays.asList(...) runs in O(1) time since it returns a fixed-size list backed by the original array (no copying).
  2. Creating a new ArrayList with new ArrayList<>(Arrays.asList(...)) runs in O(n) time because it copies all elements into a new structure.
  3. Memory: The first approach does not allocate new storage for the list elements; the second approach does.

Interview Tips & Real-World Scenarios

  1. Immutability vs. Mutability: If you need to modify the list (add or remove elements), opt for new ArrayList<>(Arrays.asList(...)).
  2. Synchronization: If you need thread safety, consider wrapping the list using Collections.synchronizedList(...) or exploring concurrent collections.
  3. Edge Cases: Converting an empty array yields an empty list. Converting an array of primitives? You’ll need to box them to their wrapper types first (e.g., int[] -> Integer[]).

Level Up Your Java Skills

For deeper insights into Java best practices, data structures, and prepping for technical interviews, check out these resources from DesignGurus.io:

If you’d like personalized feedback on your coding or system design skills, book a Mock Interview session with ex-FAANG engineers. You can also explore the DesignGurus YouTube Channel for free tutorials, system design breakdowns, and interview tips.

Bottom Line:
To convert an array to a list in Java, Arrays.asList(...) is ideal for quick conversions when you don’t need to resize the list. If you need a truly mutable list, wrap it in a new ArrayList<>(...). Choose the approach that best suits your performance needs and whether or not you want to decouple the resulting list from the original array.

CONTRIBUTOR
TechGrind