CuriousMind
CuriousMind

Reputation: 8943

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

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.

Upvotes: 45

Views: 62840

Answers (4)

Ted Hopp
Ted Hopp

Reputation: 234857

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.)

Upvotes: 82

Arman Tumanyan
Arman Tumanyan

Reputation: 426

Hashtable is synchronized but HashMap is not, but you can make HashMap synchronize with help of method Collections.synchronizedMap() . Hashtable, HashMap and HashSet are woking based on Hash Table data structure. You can use one null key and which kind of null values you want for HashMap but Hashtable does not allow null key or null values. Basically under HashSet is working HashMap where value is Object hence HashSet values are unique because HashMap keys are unique. So for putting key value pair in Hashmap or Hashtable or putting element to HashSet you need to ovveride hashcode and equals methods from Object class to correct working of this implementations. This is because under this implementations are working Hash Table algorithm and hashcode, equals is required to put values in right bucket. Its important to know that you need to use as keys String or any other wrapper class such us Integer, this is because this objects are immutable and they can be good keys for this implementations or you can create your own immutable class and use it as key. Use immutable objects as key is good practise because after creation they cant change their state hence the hashcode is same always.

Upvotes: 0

P.sharma
P.sharma

Reputation: 89

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.

Upvotes: 5

Beefster
Beefster

Reputation: 789

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.

Upvotes: 7

Related Questions