Last Updated on 11 April 2024   |   Print Email
This Java tutorial helps you understand and use the Collections utilty class for creating thread-safe collections using wrapper factory methods.You know, in addition to generic algorithms and collections factories, the java.util.Collections class provides utility methods that return wrapper objects that modify the behavior of standard collection classes such as synchronizing them, making them unmodifiable and checking the type of elements being added to them. This article helps you explore and use these functionalities properly in your Java daily coding.Let’s start with the synchronized wrappers.
1. Synchronized Collections Examples
By default, most of the classes in the Java Collections framework are not thead-safe to avoid the unnecessary overhead of synchronization (except the Vector and Hashtable are synchronized). However, when it comes to using collections in multi-threaded programs, you should use the synchronized wrappers provided by the Collections class. Below is the list of synchronizedXXX() methods that wraps a specified collection in a synchronized one:
Collection<T> synchronizedCollection(Collection<T> c)
List<T> synchronizedList(List<T> list)
Map<K,V> synchronizedMap(Map<K,V> m)
Set<T> synchronizedSet(Set<T> s)
SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m)
Is it possible to synchronize an ArrayList using the synchronizedCollection() method? Yes, of course it is possible as ArrayList is a subtype of Collection. However, the List-specific methods are not synchronized. So the more concrete method, the better. NOTE: All the returned wrappers do not synchronize the iterator() method so you should manually use a synchronized block when iterating the collection. For example:
Set<Integer> setNumbers = Collections.synchronizedSet(new HashSet<>());
setNumbers.add(2016); // synchronized call
setNumbers.add(1800); // synchronized call
// using iterator must be synchronized manually:
synchronized (setNumbers) {
Iterator<Integer> iterator = setNumbers.iterator();
while (iterator.hasNext()) {
Integer num = iterator.next();
System.out.println(num);
}
}
2. Unmodifiable Collections Examples
Sometimes you want to prevent the client from changing a collection i.e. adding and removing elements are prohibited. The Collections class provides some useful methods that wrap a specified collection in an unmodifiable one, as shown in the following list:
Collection<T> unmodifiableCollection(Collection<? extends T> c)
List<T> unmodifiableList(List<? extends T> list)
Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
Set<T> unmodifiableSet(Set<? extends T> s)
SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m)
As you can see, these unmodifiableXXX() methods are useful when you want to allow clients read-only access to the internal data structure.
3. Checked Collections Examples
The Collections class also provides the checkedXXX() methods that returns a dynamically typesafe view of the specified collection. This can be useful when passing a typed collection reference to a third-party library method which is not generic, as the checked wrappers will test every element added to the collection to ensure the collection contains no incorrectly typed elements.Checked wrappers can be created using the following methods:
Any attempt to insert an element of the wrong type will result in an immediate ClassCastException.Consider an example. The following statements create a checked list and pass it to a third-party library method:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments