You can provide either Set.of or List.of factory method, since Java 9, or Arrays.asList factory method to the ArrayList(Collection) constructor to create and init an ArrayList in one line
Apart from that, you can use add and addAll methods after the creation time to initialize an ArrayList
Let's walk through this tutorial to explore them in more details
Initialize in one line with Java 9+ List.of and Set.of
- Provide either Set.of or List.of factory method, since Java 9+, to the ArrayList(Collection) constructor to create and init an ArrayList in one line at the creation time
@Test
public void initWithListOfAndSetOf() {
List<Integer> arrayList1 = new ArrayList<>(List.of(3, 1, 2));
assertThat(arrayList1).contains(3, 1, 2);
List<Integer> arrayList2 = new ArrayList<>(Set.of(5, 4, 6));
assertThat(arrayList2).contains(5, 4, 6);
}
List.of permits duplicated elements while Set.of doesn't, it throws IllegalArgumentException
Both List.of and Set.of don't permit null element, they throw NullPointerException
Initialize in one line with Arrays.asList
Provide Arrays.asList to the ArrayList(Collection) constructor to create and init an ArrayList in one line
Array.asList accepts duplicated and null elements
@Test
public void initWithArraysAsList() {
List<Integer> set = new ArrayList<>(Arrays.asList(3, 1, 2, null));
assertThat(set).contains(3, 1, 2, null);
}
Initialize in one line from an existing Collection
- Provide an existing Collection to the ArrayList(Collection) constructor to create and init a ArrayList in one line
@Test
public void initFromAnExistingCollection() {
List<Integer> arrayList1 = new ArrayList<>(Arrays.asList(3, 1, 2, null));
List<Integer> arrayList2 = new ArrayList<>(arrayList1);
assertThat(arrayList2).contains(3, 1, 2, null);
}
Initialize by using add and addAll methods
- You can use add or addAll method to initialize an ArrayList after the creation time
@Test
public void initWithAddAndAddAll() {
List<Integer> arrayList1 = new ArrayList<>(Set.of(1, 2, 3));
List<Integer> arrayList2 = new ArrayList<>();
arrayList1.add(1);
arrayList1.add(1); // duplicated element is ignored
arrayList1.add(null); // permits null element
arrayList2.addAll(arrayList1);
assertThat(arrayList2).contains(1, 2, 3, null);
}
Double brace initialization should not be used in practice
- Double brace initialization can cause a memory leak as it creates anonymous classes with an hidden reference to the owner instance object
@Test
public void initWithDoubleBrace() {
List<Integer> arrayList = new ArrayList<>(){{
add(3);
add(1);
add(2);
}};
assertThat(arrayList).contains(1, 2, 3);
}
Initialize with capacity
- Use this to improve performance when you need to add a large number of items than the ArrayList default capacity (10)
- Internally, an ArrayList is backed by a fixed size array. When its capacity is full, Java needs to create a new array with larger capacity and copies old items to that
@Test
public void initWithCapacity() {
List<Integer> arrayList = new ArrayList<>(1000);
for (int i = 0; i < 1000; i++) {
arrayList.add(i);
}
assertThat(arrayList).hasSize(1000);
}
Conclusion
In this tutorial, we learned some ways to create and initialize an ArrayList in Java. You can find the full source code as below