We all know about about Traditional Collections ( i.e. List, Set, Queue and its implemented Classes) and Concurrent Collection (i.e. ConcurrentMap interface, ConcurrentHashMap class, CopyOnWriteArrayList class etc). In these two Collections, there are few differences like:
- Most of the Classes which are present in Traditional Collections (i.e ArrayList, LinkedList, HashMap etc) are non-synchronized in nature and Hence there is no thread-safety. But All the classes present in Concurrent Collections are synchronized in nature. Therefore In Concurrent classes, we dont have to take care about Thread-safety.
- While Traditional Collections also have some classes (like Vector, Stack etc) which are synchronized in nature and Traditional Collections also have SynchronizedSet, SynchronizedList, SynchronizedMap methods through which we can get Synchronized version of non-synchronized objects. But these above Synchronized classes are not good in terms of performance because of wide-locking mechanism .Whereas Concurrent Collections classes performance are relatively high than Traditional Collections classes.
- In the Traditional Collections, if a thread is iterating a Collection object and if another thread try to add new element in that iterating object simultaneously then we will get RuntimeException ConcurrentModificationException. Whereas In the above case, we will not get any Runtime Exception if we are Working with Concurrent Collections Classes.
- Traditional Collections classes is good choice if we are not dealing with thread in our application. whereas because of the Concurrent/Synchronized Collection we can use multiple Threads which are dealing with Collections Object. Therefore Concurrent Collections are best choice if we are dealing Multiple Threads in our application.
Exception in thread “main” java.util.ConcurrentModificationException
A B c
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Need of Concurrent Collections in java
- Collections in Java
- Collections.nCopies() in Java
- Multidimensional Collections in Java
- Collections min() method in Java with Examples
- Output of Java programs | Set 13 (Collections)
- Collections.binarySearch() in Java with Examples
- Collections max() method in Java with Examples
- Collections.reverse() in Java with Examples
- Collections.singleton() method in Java with example
- Collections.reverseOrder() in Java with Examples
- Collections.shuffle() in Java with Examples
- Collections.sort() in Java with Examples
- Java Lambda Expression with Collections
- Collections synchronizedCollection() method in Java with Examples
- Collections singletonList() method in Java with Examples
- Collections synchronizedSet() method in Java with Examples
- Collections newSetFromMap() method in Java with Examples
- Collections swap() method in Java with Examples
- Collections replaceAll() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.