AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

initialize ArrayList Java

Let’s see how to initialize an ArrayList in Java according to our needs. There are multiple ways to do it, and in this article, we will cover the most frequent and common ones.

Using Arrays.asList() to initialize an ArrayList from an Array

We can use the ArrayList constructor and pass the elements we want to initialize through asList():

ArrayList<String> fruits = new ArrayList<String>( Arrays.asList("apple", "orange", "banana") );

Using List.of from Java 9 to initialize an ArrayList in Java

We can use the static method List.of() to construct unmodifiable lists. However, we need to note that we cannot use the add() operation.

List<String> fruits = List.of("apple", "orange", "banana");

Using the Java Stream API

With the introduction of Java Streams, we can create and initialize an ArrayList using the toList() and toCollection() methods.

The toList() method returns a new list through the Collector interface with the values passed to the Stream.

List<String> fruits = Stream.of("apple", "orange", "banana").collect(Collectors.toList());

The toCollection() method creates a collection using a Collector and passes a new instance of ArrayList to it.

List<String> fruits = Stream.of("apple", "orange", "banana").collect(Collectors.toCollection(ArrayList::new));

Using the Java Collections API

Through the Collections class, we can use different methods to create a list.

  • unmodifiableList()
  • singletonList()

The Collections.unmodifiableList() method returns an unmodifiable list from a series of elements:

List<String> fruits = Collections.unmodifiableList(Arrays.asList("apple", "orange", "banana"));

Another method provided by the Java Collections API is singletonList(), which returns an unmodifiable list with a single element:

List<String> fruits = Collections.singletonList("apple");

Using the Guava library

Another approach to initializing an ArrayList in Java is using the Guava library.

The Guava library provides different methods to create immutable lists:

  • ImmutableList.of
  • ImmutableList.copyOf

For example, if we want to initialize an ArrayList in Java with ImmutableList.of, we can do it as follows:

List<String> fruits = ImmutableList.of("apple", "orange", "banana");

Another method provided by the Guava library to initialize a list in Java is ImmutableList.copyOf(), which returns a list with the specified elements:

ImmutableList<String> fruits = ImmutableList.copyOf(Arrays.asList("apple", "orange", "banana"));

As a note, ImmutableList.copyOf returns null if any of the elements are null.

Conclusión

In this article on how to initialize an ArrayList in Java, we have seen different approaches to initialize and create an ArrayList.

If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!

Leave a Reply

Your email address will not be published. Required fields are marked *