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
Java Book
Collection
Collections:
- Collections
- Get empty collection from Collections
- Do binary search
- Copy value to a List
- Get Enumeration from collection, create list from Enumeration
- Fill a list
- Get the max and min value from a Collection
- Fill n Copy object to a list
- Replace value in a list
- Reverse a list
- Get comparator in reverse order
- Rotate and shuffle a list
- Create singleton
- Sort a list
- Swap element in a list
- Get a synchronized collection
- Return an unmodifiable view of collections