Initializing ArrayList in Java in One Line: Secrets and Pitfalls

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

ArrayList in One Line in Java: From Beginner to Pro



Imagine: you're writing a test, creating a prototype, or just want to quickly define a list of strings. In Python, it looks like my_list = ["a", "b", "c"]. In Java… the headache begins. Do you really have to write three or four lines every time, calling add() for each element? Spoiler: no. In this article, we'll break down all the ways to initialize an ArrayList in one line, answer the most common questions from Stack Overflow, and show you which method to choose depending on the task. Let's go!



This material is based on a real popular Stack Overflow question that has gathered thousands of views. We've adapted it for learning, so you don't just copy the code, but understand why it works (or doesn't work).



Why is simple initialization a pain?



In Java, there is ArrayList — a dynamic array. But it doesn't have a built-in literal for creating it with data, like arrays do (String[] arr = {"a", "b", "c"};). Therefore, beginners often write like this:



ArrayList<String> list = new ArrayList<>();list.add("Java");list.add("Python");list.add("JavaScript");


This works, but it takes 4 lines. What if there are 10 elements? 100? You want something more compact. Let's break down all the legal (and not-so-legal) ways to do this in one line.



Method 1: The Classic — Arrays.asList()



The most popular method you'll find in Stack Overflow answers:



ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));


Here, we first create a fixed-size list using Arrays.asList(), and then pass it to the ArrayList constructor. The result is a fully mutable list. You can add, remove, and modify elements.



Important: Arrays.asList() itself does not return an ArrayList! It returns its own internal implementation of a fixed-size list. If you try to call add() on the result of Arrays.asList(), you'll get an UnsupportedOperationException. Remember this — it's the most common question in interviews and Stack Overflow threads.



When to use?

  • When you need a mutable list.
  • When you're working with Java 7 and below (before Java 9).


Method 2: Java 9+ — List.of()



Starting with Java 9, the factory method List.of() appeared:



List<String> immutableList = List.of("a", "b", "c");


Short, elegant, concise. But there's a catch: the resulting list is immutable. You cannot add or remove an element. If you try, you'll get UnsupportedOperationException.



If you specifically need an ArrayList, wrap it:



ArrayList<String> list = new ArrayList<>(List.of("a", "b", "c"));


This method is preferable to Arrays.asList() if you're using Java 9 or higher, because List.of() returns a structurally immutable list (you cannot change the size), unlike Arrays.asList(), which allows modifying elements via set().



When to use?

  • Java 9+
  • You need a short syntax
  • You don't plan to modify the list (or you wrap it in an ArrayList)


Method 3: Double Brace Initialization — an Anti-pattern



This trick often appears on Stack Overflow. It looks like this:



ArrayList<String> list = new ArrayList<>() {{    add("a");    add("b");    add("c");}};


What's happening here? We're creating an anonymous inner class that inherits from ArrayLi

Blogs

Book Recommendations