Return an unmodifiable view of collections

static<T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
Returns an unmodifiable view of the specified collection.
static<T> List<T> unmodifiableList(List<? extends T> list)
Returns an unmodifiable view of the specified list.
static<K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
Returns an unmodifiable view of the specified map.
static<T> Set<T> unmodifiableSet(Set<? extends T> s)
Returns an unmodifiable view of the specified set.
static<K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m)
Returns an unmodifiable view of the specified sorted map.
static<T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)
Returns an unmodifiable view of the specified sorted set.

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

public class Main {

  public static void main(String args[]) {
    List<Character> list = new ArrayList<Character>();

    list.add('X');

    System.out.println("Element added to list: " + list.get(0));

    Collection<Character> immutableCol = Collections.unmodifiableCollection(list);

    immutableCol.add('Y');
  }
}
  

The output:


Element added to list: X
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.add(Collections.java:1018)
    at Main.main(Main.java:17)

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

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

    List<String> list = new ArrayList<String>();
    list.add("A");
    list.add("B");
    list.add("C");
    list = Collections.unmodifiableList(list);
    list.add(1, "G");

    System.out.println(list);

  }
}
  

The output:


Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableList.add(Collections.java:1159)
    at Main.main(Main.java:13)

import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;

public class Main {
  public static void main(String[] s) {
    Hashtable<String,String> table = new Hashtable<String,String>();
    table.put("key1", "value1");
    table.put("key2", "value2");
    table.put("key3", "value3");

    Map m = Collections.unmodifiableMap(table);

    m.put("key3", "value3");

    System.out.println(m);

  }
}
  

The output:


Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.put(Collections.java:1285)
    at Main.main(Main.java:14)

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    Set OBJECT_SET;

    Set objectSet = new HashSet();
    objectSet.add(new Integer(5));
    objectSet.add(null);
    OBJECT_SET = Collections.unmodifiableSet(objectSet);

    final Iterator iter = OBJECT_SET.iterator();
    Object obj = null;

    while (iter.hasNext()) {
      obj = iter.next();
      if (obj instanceof Number) {
        System.out.println(obj);
      }
    }
  }
}

The output:


5
Home 
  Java Book 
    Collection  

Collections:
  1. Collections
  2. Get empty collection from Collections
  3. Do binary search
  4. Copy value to a List
  5. Get Enumeration from collection, create list from Enumeration
  6. Fill a list
  7. Get the max and min value from a Collection
  8. Fill n Copy object to a list
  9. Replace value in a list
  10. Reverse a list
  11. Get comparator in reverse order
  12. Rotate and shuffle a list
  13. Create singleton
  14. Sort a list
  15. Swap element in a list
  16. Get a synchronized collection
  17. Return an unmodifiable view of collections