Java Collection Tutorial - Java Collections .unmodifiableCollection ( Collection <? extends T> c)








Syntax

Collections.unmodifiableCollection(Collection <? extends T> c) has the following syntax.

public static <T> Collection <T> unmodifiableCollection(Collection <? extends T> c)

Example

In the following code shows how to use Collections.unmodifiableCollection(Collection <? extends T> c) method.

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/*  w w w . j  a va2s . c o m*/
public class Main {
   public static void main(String[] args) {
      // create array list
      List<Character>  list = new ArrayList<Character> ();
      
      // populate the list
      list.add('X');
      list.add('Y');
      
      System.out.println("Initial list: "+ list);
      
      Collection<Character>  immutablelist = Collections.unmodifiableCollection(list);
      
      // try to modify the list
      immutablelist.add('Z');      
   }
}

The code above generates the following result.