Collection details in Java
How can you get synchronized standard collection objects? Using the static methods synchronizedMap() and synchronizedList() of the Collections class. These methods return the synchronized decorator of the passed collection. However, manual synchronization is still required in the case of a collection traversal. Map m = Collections.synchronizedMap(new HashMap()); List l = Collections.synchronizedList(new ArrayList()); Since Java 6, JCF has been extended with special collections that support multithreading, such as CopyOnWriteArrayList and ConcurrentHashMap. How do I get a read-only collection? With help: Collections.unmodifiableList(list); Collections.unmodifiableSet(set); Collections.unmodifiableMap(map). These methods take a collection as a parameter and return a read-only collection with the same elements inside. Single threaded program that causes the collection to throw a ConcurrentModificationException. public static void main(String[] args) { List<Inte...