Explain Codes LogoExplain Codes Logo

What is the difference between List.of and Arrays.asList?

java
thread-safety
immutable-lists
list-of-vs-arrays-aslist
Anton ShumikhinbyAnton Shumikhin·Nov 10, 2024
TLDR

List.of returns an immutable list directly—no modifications allowed post-creation. Arrays.asList gives a mutable view of an array—elements can be replaced, but no size alteration (add/remove).

List.of

List<String> immutableList = List.of("a", "b", "c"); // This is the list equivalent of "one order of immutableCocktail, please!" 🍸

Arrays.asList

List<String> mutableList = Arrays.asList("a", "b", "c"); mutableList.set(1, "d"); // It's a bit like replacing fries with a pizza slice. It's doable. Dough-able? 😄

Thread safety, null elements and their behaviour

While List.of is thread-safe, providing a safe habitat for your code to thrive in a multi-threading environment, Arrays.asList, the adventurous cousin, is not inherently thread-safe. So if you choose to use Arrays.asList in concurrent scenarios, remember to wear your safety gear!

Next up, List.of promises a null-free zone, so if your intention is to prevent nulls from trespassing, it's your go-to function. On the contrary, Arrays.asList allows null elements, so if your data stories include the mysterious null character, Arrays.asList will let them in.

Changes in the original array

Now let's talk reflectiveness. When Arrays.asList is used, the list generated is backed by the original array:

String[] myArray = {"java", "python", "ruby"}; List<String> myList = Arrays.asList(myArray); myArray[0] = "kotlin"; System.out.println(myList.get(0)); // Prints "kotlin", not "java". Plot twist, eh! 🎞️

However, List.of operates independently, being completely unaffected by changes in the original array.

Use-cases for both

  • Use List.of when you need your list to stay as firm as a statue - no changing, no resizing. It's perfect for constant values.
  • Use Arrays.asList when you want your list to be as flexible as a yoga practitioner - elements can be changed, but it cannot grow or shrink.

Serialization issues and wonders

If Arrays.asList were a wizard, it would cast Serialization spells - allowing the state of the list to get persisted and retrieved later. However, the same can't be said about List.of, at least not in any environment running on JDK versions earlier than 9.

Memory considerations and resizing

Notably, immutable lists from List.of are typically more memory-efficient since they simply don't have the overhead required for mutability. Plus, they're safe to use in concurrent environments.

Lists from Arrays.asList are fixed-size. Size changes are as welcoming as pineapple on pizza to some folks – they're just not permitted!

Best practices

List.of to use when you need:

  • A small and unchanging list.
  • To safeguard from unintended side-effects with immutability.
  • Read-only constants to avoid unwanted mutations.

Employ Arrays.asList:

  • If you want a modifiable list from an existing array.
  • When needing a quick-relief solution with mutable elements but fixed size.
  • If it's important for serialization to be on the table.