Which collection classes are thread-safe

Which collection classes are thread-safe

Below collection classes which are thread-safe:

  • HashTale: HashTable is basically a data structure that used to implement of an associative array which maps keys to values. We can use non-null object as a key or as a value.JDK version 1.2 HashTable class was retrofitted to implement the Map interface and made member of Java Collections Framework. Hashtable is a synchronized class. This class should be used carefully and it is recommended to use HashMap in place of Hashtable if thread-safe implementation is not needed .If very highly thread-safe concurrent implementation is anticipated then it is good to use ConcurrentHashMap in place of Hashtable.
  • Vector: JDK version 1.2 Vector class was retrofitted to implement the List interface and made member of Java Collections Framework. , Vector is a synchronized class. This class should be used carefully and it is recommended to use  ArrayList in place of Vector. If thread-safe implementation is not needed and very highly thread-safe concurrent implementation is anticipated then it is good to use CopyOnWriteArrayList in place of Vector.
  • Stack: Stack class is available since JDK 1.0 and extends Vector class with five operations that allows vector to be treated as stack. This class represents LIFO (last-in-first-out) stack of objects. The operation push and , as well as also method to peek at top of the item on stack, a method for test whether stack is empty or not, and a method that used to search stack for an item and to discover how far it’s from top.Initially when stack class is first created it contains no items.Java has also introduced a more complete and very consistent set of LIFO stack operations is provided by Deque interface and through its implementations that should be used in preference to Stack class.
    Below is example:
    Deque<Integer> stack = new ArrayDeque<Integer>();
  • Properties: Properties class is available since JDK 1.0 and extends Hashtable<Object,Object> class. It’s thread-safe and multiple threads could share single Properties object without any need of the external synchronization.

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *