What is the difference between `Collectors.toUnmodifiableList()` and `Collections.unmodifiableList(Collectors.toList())`?

Java Professional Hard

Java Professional — Hard

What is the difference between `Collectors.toUnmodifiableList()` and `Collections.unmodifiableList(Collectors.toList())`?

Key points

  • `toUnmodifiableList()` ensures immutability and no nulls in Java 10+
  • `unmodifiableList(toList())` wraps a mutable list and allows null values
  • Understanding the distinction is important for proper list handling in Java

Ready to go further?

Related questions