You can create and initialize an ArrayList by using its constructors, factory methods of Collection class or Java 9+ List.of() and List.copyOf()

Using ArrayList's constructors

  • Create a new empty ArrayList
List<Integer> lst = new ArrayList<>();  

ArrayList is backed by a resizable array with the default initial capacity is 10

  • Create a new empty ArrayList with the specified initial capacity
List<Integer> lst = new ArrayList<>(20);  
  • Create and init a new ArrayList with the same elements as the specified collection, in the order returned by the collection's iterator
List<Integer> lst1 = new ArrayList<>(List.of(1, 2, 3));  
System.out.println(lst1); // [1, 2, 3]  
List<Integer> lst2 = new ArrayList<>(Set.of(1, 2, 3));  
System.out.println(lst2); // [3, 2, 1] - this output will be vary as Set has no guarantees on the order  

List.of and Set.of are Java 9+ APIs, you can see List.of in more details in the later part of this tutorial

  • Create and init an ArrayList in one line with double brace initialization (should not be used)
List<Integer> lst = new ArrayList<Integer>(){{  
    add(1);
    add(2);
    add(3);
}};

Double brace initialization can lead to memory leaks as it creates an anonymous class with a reference to the owner object

  • Create and init an ArrayList in multiple lines
List<Integer> lst = new ArrayList<>();  
lst.add(1);  
lst.add(2);  
lst.add(3);  


Using Arrays.asList

  • Create and init an ArrayList in one line with Arrays.asList
List<Integer> lst = Arrays.asList(1, 2, 3);  
System.out.println(lst); // [1, 2, 3]  

Arrays.asList returns a fixed size list so you can not add more elements to the list after creation, UnsupportedOperationException will be thrown if you attempt to do so


Using Collections class

Collections class provide some factory methods to create and init immutable ArrayList in one line

UnsupportedOperationException will be thrown if attempting to add, remove, update or sort elements in the list after creation

  • Create a new empty immutable (unmodifiable) and serializable list with Collections.emptyList()
List<Integer> lst = Collections.emptyList();  
  • Create and init an immutable and serializable list containing only the specified element Collections.singletonList(T o)
List<Integer> lst = Collections.singletonList(1);  


Using List.of and List.copyOf with Java 9+

Java 9+ provides List.of and List.copyOf factory methods to create and init an immutable list in one line

NullPointerException will be thrown if attempting to init with null elements

UnsupportedOperationException will be thrown if attempting to add, remove, update or sort elements in the list after creation

  • List.of(E... elements) method returns an immutable list containing an arbitrary number of elements
List<Integer> immutableList = List.of(1, 2, 3);  
  • List.copyOf(Collection) method returns an immutable list containing the elements of the given collection in its iteration order
List<Integer> lst1 = List.of(1, 2, 3)  
List<Integer> lst2 = List.copyOf(lst1);