Hashtable, HashMap, HashSet , hash table concept in Java collection framework

42,135

Solution 1

Java's Set and Map interfaces specify two very different collection types. A Set is just what it sounds like: a collection of distinct (non-equal) objects, with no other structure. A Map is, conceptually, also just what it sounds like: a mapping from a set of objects (the distinct keys) to a collection of objects (the values). Hashtable and HashMap both implement Map, HashSet implements Set, and they all use hash codes for keys/objects contained in the sets to improve performance.

Hashtable and HashMap

Hashtable is a legacy class that almost always should be avoided in favor of HashMap. They do essentially the same thing, except most methods in Hashtable are synchronized, making individual method calls thread-safe.1 You have to provide your own synchronization or other thread safety mechanism if you are using multiple threads and HashMap.

The problem with Hashtable is that synchronizing each method call (which is a not-insignificant operation) is usually the wrong thing. Either you don't need synchronization at all or, from the point of the view of the application logic, you need to synchronize over transactions that span multiple method calls. Since it was impossible to simply remove the method-level synchronization from Hashtable without breaking existing code, the Collections framework authors needed to come up with a new class; hence HashMap. It's also a better name, since it becomes clear that it's a kind of Map.

Oh, if you do need method-level synchronization, you still shouldn't use Hashtable. Instead, you can call Collections.synchronizedMap() to turn any map into a synchronized one. Alternatively, you can use ConcurrentHashMap, which, according to the docs: "obeys the same functional specification as Hashtable" but has better performance and additional functionality (such as putIfAbsent()).

1 There are other differences (less significant, in my view) such as HashMap supporting null values and keys.

HashSet

In terms of functionality, HashSet has nothing to do with HashMap. It happens to use a HashMap internally to implement the Set functionality. For some reason, the Collections framework developers thought it would be a good idea to make this internal implementation detail part of the public specification for the class. (This was an error, in my view.)

Solution 2

Hashtable was an old class that was created before Java had generics. It's only around still for backwards compatibility. Use HashMap instead.

Use HashSet when you don't need to map keys to values. It's built on the same algorithm as hash tables, but it is used for a fundamentally different purpose.

Solution 3

HashMap and HashTable both inherits Map interface.and have almost same working and properties.But the major differences are as follows:-

1.Hashmap is an unordered map of key and value pairs.And we can have the null key or value pairs inside a hashmap.Also a hashmap is unsynchronized(i.e. not thread safe multiple threads can access and modify it at the same time.)But we can externally make a hashmap thread-safe.So if we are not considering the synchronization issues then hashmap is preferable.

2.HashTable:- A synchronized hashMap(i.e. a thread safe hashmap).But the keys and value pairs in this case never ever be null.In a Hashtable we specify an object that is used as a key, and the value we want to associate to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table

3.HashSet:-A hashset inherits set interface and in the end its also based on hashtable(or we can say deeply connected to our hashmap only)but in this case the key's and values pairs are always unique no duplicate values allowed.but null key values are allowed.Objects are inserted based on their hash code.

In a conclusion we can say all the three collections have connected to Map interface on and all.

Share:
42,135
CuriousMind
Author by

CuriousMind

Updated on July 27, 2020

Comments

  • CuriousMind
    CuriousMind almost 4 years

    I am learning Java Collection Framework and got moderated understanding. Now, when I am going a bit further I got some doubts in: HashMap, HashSet, Hashtable.

    The Javadoc for HashMap says:

    Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key.

    The Javadoc for HashSet says:

    This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

    The Javadoc for Hashtable says:

    This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.

    It is confusing that all of them implement the hash table. Do they implement the concept of hash table?

    It seems that all these are related to each other, but I am not able to fully understand it.

    Can anyone help me understand this concept in simple language.