Java: Unmodifiable Collection Methods and Example

In Collection Framework, to make collection type object as unmodifiable java.Util.Collections class provides static methods to make these object as unmodifiable.

Collections Class Unmodifiable Methods

static Collection
unmodifiableCollection(Collection<? extends T> c)
Returns an unmodifiable view of the specified collection.
static List
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 <K,V> NavigableMap<K,V>
unmodifiableNavigableMap(NavigableMap<K,? extends V> m)
Returns an unmodifiable view of the given map.
static NavigableSet
unmodifiableNavigableSet(NavigableSet s)
Returns an unmodifiable view of the given navigable set.
static Set
unmodifiableSet(Set<? extends T> s)
Returns an unmodifiable view of the given set.
static <K,V> SortedMap<K,V>
unmodifiableSortedMap(SortedMap<K,? extends V> m)
Returns an unmodifiable view of the given map.
static SortedSet
unmodifiableSortedSet(SortedSet s)
Returns an unmodifiable view of the given  set.

Example: Synchronized Collections

In this example creating the blank type of collection and making it Unmodified. You can assign the same with collection objects.

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;

public class CollectionUnmodifiedExample {

	public static void main(String[] args) {
		Collection c = new ArrayList();
	    Collections.unmodifiableCollection(c);
	    Collections.unmodifiableList(new ArrayList());
	    Collections.unmodifiableMap(new HashMap());
	    Collections.unmodifiableSet(new HashSet());
	}

}

References