List.of() Vs Arrays.asList

Understanding the Distinction between Java’s List and Arrays Factory Methods

MehmoodGhaffarMemon
3 min readJun 7, 2023
what is the difference between list.of and Arrays.asList factory methods in java.

Introduction:

Java provides several convenient methods for creating lists, including List.of() and Arrays.asList(). While both methods allow you to create lists easily, they exhibit some key differences. In this article, we’ll explore the disparities between List.of() and Arrays.asList() in Java, highlighting their varying behaviors, use cases, and implications. By the end, you’ll have a clear understanding of when to use each method and how they differ from one another.

List.of():

List.of() is a factory method introduced in Java 9 that creates an immutable list containing the specified elements. Here are some key points to note:

  • Immutability: The resulting list is immutable, meaning its size and elements cannot be modified after creation.
  • Fixed-size: The list created by List.of() has a fixed size and does not support adding or removing elements.
  • Null values: List.of() does not allow null elements. If you attempt to include null, it will throw a NullPointerException.
  • Example:
List<String> immutable_list = List.of("apple", "banana", "orange");

Arrays.asList():

Arrays.asList() is a method available since the early versions of Java and provides a convenient way to create a fixed-size list backed by the specified array. Let’s examine its characteristics:

  • Modifiability: The resulting list from Arrays.asList() is modifiable, allowing you to only update its elements and not its structure.
  • Backed by an array: The list is backed by the original array, so any changes to elements of the list will affect the underlying array and vice versa.
  • Fixed-size limitation: Although modifiable, the size of the list returned by Arrays.asList() is fixed, preventing structural modifications such as adding or removing elements.
  • Null values: Unlike List.of(), Arrays.asList() allows null elements.
  • Example:
List<String> mutable_list = Arrays.asList("red", "green", "blue");

Use Cases:

Now that we understand the differences between List.of() and Arrays.asList(), let’s explore their respective use cases:

List.of():

This method is ideal when you need an immutable list with a fixed set of elements. It ensures data integrity and prevents accidental modifications.

import java.util.List;

public class ListOfExample {
public static void main(String[] args) {
String[] colorsArray = { "Red", "Green", "Blue" };
List<String> colors = List.of(colorsArray);

colorsArray[0] = "Yellow";

// Accessing elements in the original array
System.out.println(colors.get(0).equals(colorsArray[0])); // Output: false
System.out.println(colors.get(1).equals(colorsArray[1])); // Output: true
System.out.println(colors.get(2).equals(colorsArray[2])); // Output: true
}
}

In the above example, List.of() is used to create an immutable list of colors. Any attempt to modify the list by adding or removing elements will result in exceptions being thrown. The example also demonstrates accessing elements from the list.

Arrays.asList():

Use this method when you want a fixed-size (serializable) list backed by specified array. Any changes to the returned list will be write through to the original array as well.

import java.util.Arrays;
import java.util.List;

public class ArraysAsListExample {
public static void main(String[] args) {
String[] colorsArray = {"Red", "Green", "Blue"};
List<String> colors = Arrays.asList(colorsArray);

// Modifying the list (and array)
colors.set(0, "Yellow");

// Accessing elements in the original array
System.out.println(colors.get(0).equals(colorsArray[0])); // Output: true
System.out.println(colors.get(1).equals(colorsArray[1])); // Output: true
System.out.println(colors.get(2).equals(colorsArray[2])); // Output: true
}
}

Conclusion:

Understanding the disparities between List.of() and Arrays.asList() is essential for Java developers. While List.of() creates an immutable list with a fixed size, Arrays.asList() produces a modifiable list backed by an array. By considering the characteristics, use cases, and implications of each method, you can make informed decisions when selecting the appropriate factory method for your specific programming needs.

--

--

MehmoodGhaffarMemon

Tech-savvy writer sharing knowledge on fullstack-dev, DevOps, and ethical hacking. Bringing insights and solutions to fellow tech enthusiasts