Creating singleton collections

The Collections class in the java.util package provides convenient methods to create singleton collection objects. A singleton collection object is a List, Map or Set that contains only a single element. Here’s how you can create a singleton set:

Set set = Collections.singleton( "The Element" );

There are 2 more methods which allow you to create a singleton list or map:

List list = Collections.singletonList( "The Element" );
Map map = Collections.singletonMap( "Key" , "Value" );

The collection object returned in all these methods is an immutable object. Also, all 3 of them are static methods in the Collections class.

Creating unmodifiable & synchronized collections

By default, the classes available in the Java Collections framework are read/write enabled & not thread safe. Sometimes the requirement is to have read only i.e. unmodifiable collections. The Collections class available in the java.util package provides utility methods to create such collections. Note: Do not confuse this class with the Collection interface which is the root interface of the collection hierarchy.

The objects returned by these methods are wrappers over the original collection object you supply to the method (Decorator design pattern).

The Collections class has 6 static methods that return a read only collection object. These are:

  • static Collection unmodifiableCollection(Collection c)
  • static List unmodifiableList(List list)
  • static Map unmodifiableMap(Map m)
  • static Set unmodifiableSet(Set s)
  • static SortedMap unmodifiableSortedMap(SortedMap m)
  • static SortedSet unmodifiableSortedSet(SortedSet s)

It is important that you don’t store the reference to the original object, otherwise you’ll still be able to access the original object and thus the collection will not be unmodifiable. Therefore, it is best to first populate the collection with the desired values and then replace the original reference with the read-only reference e.g.

List list = new Arraylist();
// populate the list
list = Collections.unmodifiableList(list);

The classes available in the collections package are not thread safe by default. The designers of the Java collections hierarchy did this on purpose since synchronized collection classes are slower and you don’t require synchronization always. The Collections class has 6 utility static methods to wrap collection objects into synchronized ones. These methods are:

  • static Collection synchronizedCollection(Collection collection)
  • static List synchronizedList(List list)
  • static Map synchronizedMap(Map map)
  • static Set synchronizedSet(Set set)
  • static SortedMap synchronizedSortedMap(SortedMap map)
  • static SortedSet synchronizedSortedSet(SortedSet set)

It is best to create a collection object and then wrap it in a synchronized one immediately. Also, you should not keep the reference to the original object otherwise you’ll be able to access it in a thread-unsafe manner. The best way to do this is to create a synchronized collection object as follows:

List list = Collections.synchronizedList(new ArrayList());