Java Collection How to - Test if a List is Unmodifable List








Question

We would like to know how to test if a List is Unmodifable List.

Answer

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/* w  w w . j av a  2  s.  com*/
public class Main {
  public static void main(String[] args) {
    List<Integer> list = new LinkedList<Integer>();
    list.add(1);
    List<Integer> unmodifiableList = Collections.unmodifiableList(list);
    System.out.println(unmodifiableList.getClass());
    if (unmodifiableList.getClass().getName().contains("UnmodifiableList"))
      System.out.println(true);
  }
}

The code above generates the following result.