If you invoke one of the sorting or searching methods that relies on ordering on a list that contains objects that
are not mutually comparable, or are not comparable by the relevant Comparator, you will get a
ClassCastException.
You can ask how many times an element appears in a collection:
public static intfrequency(Collection<?> coll, Object elem)
Returns the number of times that the given element appears in the given
collection.
There are also methods to create singleton collectionscollections containing only a single element:
public static <T> Set<T>singleton(T elem)
Returns an immutable set containing only elem.
public static <T> List<T>singletonList(T elem)
Returns an immutable list containing only elem.
public static <K,V> Map<K,V>singletonMap(K key, V value)
Returns an immutable map containing only one entry: a mapping from key
to value.
And finally, there are methods that return empty collections:
public static <T> List<T>emptyList()
Returns an empty, immutable list of the desired type.
public static <T> Set<T>emptySet()
Returns an empty, immutable set of the desired type.
public static <K,V> Map<K,V>emptyMap()
Returns an empty, immutable map of the desired type.
There are also legacy static final fields, EMPTY_LIST, EMPTY_SET, and EMPTY_MAP, of the raw types
List, Set, and Map, initialized to refer to empty immutable collection objects. The use of these fields is not
type-safe and is discouraged, as is all use of raw types.
21.10.2. The Unmodifiable Wrappers
The Collections class has static methods that return unmodifiable wrappers for all of the collection types:
unmodifiableCollection, unmodifiableSet, unmodifiableSortedSet,
unmodifiableList, unmodifiableMap, and unmodifiableSortedMap. The collections returned
by these methods pass non-modifying methods through to the underlying collection. Modifying methods
throw UnsupportedOperationException. The unmodifiable wrapper is backed by the original
collection, so any changes you make to the collection will be visible through the wrapped collection. In other
words, the contents of an unmodifiable wrapper can change, but not through the wrapper itself.