-
Step 1
There are 3 important Hash classes available in Java framework. It is very important to understand the when to use what for the better performance of your java application.
-
Step 2
HashSet: HashSet is a set of unique elements. Unlike to List classes (ArrayList, Vector etc.), Set classes do not accept duplicate elements. If you try to add the same element again, it will not create any error, but it will overwrite the existing same element.
-
Step 3
HashMap: HashMap is a unique class in which you can store key-value pairs of objects. It accepts only one null key. It is not thread-safe, so more than one threads can access and modify the elements of HashMap. As it is not thread-safe, it is faster to access the elements. It is a widely used Hashing algorithm based Java class.
-
Step 4
Hashtable: Similar to HashMap, Hashtable can also store key-value pairs of objects. There are two major differences between HashMap and Hashtable.
1) Hashtable is thread-safe
2) Hashtable does not accept any null key
As it is providing extra feature of thread-safe, it should be used very carefully because too much use of Hashtable can lower your application performance because at a time only one thread can access a Hashtable object. -
Step 5
It is very important to decide what you want in your application, thread-safety or better performance. For better performance, use HashMap and for thread-safety, use Hashtable. In List hierarchy, there is one more thread-safe class called Vector. Similar to Hashtable, it provides thread-safety, but it may lower the performance of your application. If you don't need key-value pairs of object, you can use ArrayList (duplicate elements are allowed), Vector (for thread safety, duplicate elements are allowed) or HashSet (duplicate elements are not allowed).
-
Step 6
For sample codes of Java packages, visit
http://java-techsavy.blogspot.com/
It is an excellent website for Java sample codes. Make sure to visit it.











