How to Initialize ArrayList in Java

  • Post last modified:December 15, 2022
  • Reading time:3 mins read

Introduction

  • The Java collection framework provides many data structures to store elements, be it Array, Set, Map, Stack, or Queue.
  • Initializing these data structures is a common step while building scalable software in Java.
  • In this article, we will look at various ways to initialize the ArrayList data structure.

Initialization Methods

1: Using Arrays.asList method

  • Arrays util class provides various methods that we can use to operate on Array data structure. One popular method is converting Array into List with Arrays.asList method.
List<String> countries;
countries = Arrays.asList("EUR", "USD", "INR", "JPY");

2: Using anonymous inner class

  • We can also initialize ArrayList by using anonymous class.
List<String> countries;
countries = new ArrayList<>(){{
             add("EUR");
             add("INR");
             add("USD");
             add("JPY");
}};

3: Common way of initializing 

  • One of the common ways that many developers initialize ArrayList is by creating an ArrayList object and then calling add method.
List<String> countries;
countries = new ArrayList<>();
countries.add("EUR");
countries.add("USD");
countries.add("INR");
countries.add("JPY");

4: Initialize ArrayList using List.of method

  • Java 9+ provides factory methods that return an immutable collection. In order to get an immutable list, we can use List.of method.
List<String> countries;
countries = List.of("EUR", "USD", "INR", "JPY");

5: Initialize ArrayList from array

  • We can initialize ArrayList from array as well. we can just pass it as parameter to the ArrayList constructor.
List<String> countries;
String[] countryArr = {"EUR", "USD", "INR", "JPY"};
countries = Arrays.asList(countryArr);

6: Initialize ArrayList by streaming array

  • If you like lambda and stream pattern then we can stream array using Arrays.stream and then collect to List.
List<String> countries;
countries = Arrays.stream(countryArr).collect(Collectors.toList());

7: Initialize ArrayList from Set

  • We can initialize ArrayList from set as well. we just have to pass it as constructor parameter to the ArrayList.
List<String> countries;
Set<String> set = Set.of("EUR", "USD", "INR", "JPY");
countries = new ArrayList<>(set);

8: Initialize ArrayList from Stack

  • We can initialize ArrayList from Stack. We can pass stack as constructor parameter to ArrayList.
 List<String> countries;
 Stack<String> stack = new Stack<>(){{
            push("EUR");
            push("USD");
            push("INR");
            push("JPY");
 }};

countries = new ArrayList<>(stack);

Conclusion

  • In this article, we explore various ways to initialize ArrayList. In Java, we can use traditional methods as well as new java features to initialize ArrayList.
  • My personal favorite is the List. of factory method to get the immutable list, but if I need a mutable list then I would use Arrays.asList.

Bonus Tip

Leave a Reply