Listfruits = new ArrayList<>(); fruits.add("apple"); fruits.add("banana"); fruits.add("orange"); List unmodifiableFruits = Collections.unmodifiableList(fruits);
ListIn this example, we use the Arrays.asList method to create a new list of numbers and then create an unmodifiable view of it using the unmodifiableList method. This could be useful if we want to pass a list of numbers to a method and ensure that the list cannot be modified within that method. The Collections class is part of the java.util package library.numbers = Arrays.asList(1, 2, 3, 4, 5); List unmodifiableNumbers = Collections.unmodifiableList(numbers);