Java Language List Factory Method Examples


Example

  • List<Integer> immutableEmptyList = List.of();
    • Initializes an empty, immutable List<Integer>.
  • List<Integer> immutableList = List.of(1, 2, 3, 4, 5);
    • Initializes an immutable List<Integer> with five initial elements.
  • List<Integer> mutableList = new ArrayList<>(immutableList);
    • Initializes a mutable List<Integer> from an immutable List<Integer>.