Java List convert to read-only List

Description

Java List convert to read-only List


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
  public static void main(String[] argv) throws Exception {

    List<String> stuff = Arrays.asList(new String[] { "CSS", "HTML" });

    List<String> list = new ArrayList<>(stuff);
    list = Collections.unmodifiableList(list);

    try {//from  w  w w .  ja  v  a  2 s .  c o m
      list.set(0, "new value");
    } catch (UnsupportedOperationException e) {
      System.out.println("Got Unsupported Operation Exception");
    }

  }
}



PreviousNext

Related