While hash map uses the entering class and the concept of hash code for storing keyvalue pairs, hash set uses an internal hashmap instance where elements of the hashset being stored as keys of this internal hash map.

Working of hash map

The put method

When we try to insert a keyvalue pair into the hash map, the hash code of the object is calculated and is used as a bucket for storing the address of value object. This is the only reason for which it is recommended that every object of the class should have a unique hash code. If multiple objects have the same hash code then it will result in collision of hash codes and hence a single bucket will store address for multiple value objects.

The hash map class contains an internal class named as a Entry which corresponds to a key. All the key value pairs are stored in an array of type Entry.

The get method

When we try to retrieve a value object by passing the key object to the get method of hash map, the hash code of the key object is calculated and the corresponding bucket is looked up to get the address of value object. If there was a collision when storing the keyvalue pair, the equals method of the key object is used to calculate the right address of value object.

It is important to note here that if the hash code of key object was different when using the put method than the get method, we shall see unexpected output.

Iterating the hash map


There is no direct way to get a list of value objects stored in a hash map. Instead, we need to iterate the keys and then invoke the get method on hash map with the key object to get the corresponding value object.

There are multiple ways in which a hash map can be iterated as discussed below:

  • Invoke the entrySet method on the hashmap to get an instance of type snt with elements of type Entry. Each Entry object contains only one key and one value object. This entry set can be iterated by using the iterator and get key and value objects.
  • Another way of iterating the hash map involves getting a set of keys by invoking the keyset method on the hash map. Once this set is available, it can be iterated and with iteration the get method of hash map can be invoked by passing the current key object.

Working of hash set

The hash set class in Java contains an instance member variable of type hashmap. The hashset class utilizes the property of hash map which says that the keys of hash map class are always unique. The elements of hash set are stored as keys of this internal hash map with each key bound to the same dummy value object.

The add method of hashset class is shown below:

    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

The iterator method of hashset class is shown below:

    public Iterator<E> iterator() {
        return map.keySet().iterator();
    }

If you understand the hash map class then understanding the HashSet class is not difficult. Since the hash map class uses the hash code for storing keyvalue pairs, the HashSet class automatically uses hash based storage.

Related Posts

How do HashMap and HashSet work in Java admin Core Java
While hash map uses the entering class and the concept of hash code for storing keyvalue pairs, hash set uses an internal hashmap instance where elements of the hashset being stored as keys of this internal hash map. Working of hash map The put method When we try to insert a keyvalue...
While hash map uses the entering class and the concept of hash code for storing keyvalue pairs, hash set uses an internal <a href="http://www.javaexperience.com/differences-between-hashmap-and-hashtable-in-java/" title="Differences between HashMap and Hashtable in Java">hashmap instance</a> where elements of the hashset being stored as keys of this internal hash map. <h2>Working of hash map</h2> <h3>The put method</h3> When we try to insert a keyvalue pair into the hash map, <a href="http://www.javaexperience.com/how-to-generate-hashcode-method-for-a-class-in-java/" title="How to generate hashcode method for a class in Java">the hash code of the object</a> is calculated and is used as a bucket for storing the address of value object. This is the only reason for which it is recommended that <strong>every object of the class should have a unique hash code</strong>. If multiple objects have the same hash code then it will result in collision of hash codes and hence a single bucket will store address for multiple value objects. The hash map class contains an internal class named as a Entry which corresponds to a key. All the key value pairs are stored in an array of type Entry. <h3>The get method</h3> When we try to retrieve a value object by passing the key object to the get method of hash map, the hash code of the key object is calculated and the corresponding bucket is looked up to get the address of value object. If there was a collision when storing the keyvalue pair, <strong>the equals method of the key object is used to calculate the right address of value object</strong>. It is important to note here that if the hash code of key object was different when using the put method than the get method, we shall see unexpected output. <h3>Iterating the hash map</h3> <a name="iteratemap"></a> There is no direct way to get a list of value objects stored in a hash map. Instead, we need to iterate the keys and then invoke the get method on hash map with the key object to get the corresponding value object. There are multiple ways in which a hash map can be iterated as discussed below: <ul> <li>Invoke the entrySet method on the hashmap to get an instance of type snt with elements of type Entry. Each Entry object contains only one key and one value object. This entry set can be iterated by using the iterator and get key and value objects.</li> <li>Another way of iterating the hash map involves getting a set of keys by invoking the keyset method on the hash map. Once this set is available, it can be iterated and with iteration the get method of hash map can be invoked by passing the current key object.</li> </ul> <h2>Working of hash set</h2> The hash set class in Java contains an instance member variable of type hashmap. The hashset class utilizes the property of hash map which says that <strong>the keys of hash map class are always unique</strong>. The elements of hash set are stored as keys of this internal hash map with each key bound to the same dummy value object. The add method of hashset class is shown below: 1 The iterator method of hashset class is shown below: 1 If you understand the hash map class then understanding the HashSet class is not difficult. Since the hash map class uses the hash code for storing keyvalue pairs, the HashSet class automatically uses hash based storage.
Setting initialcapacity of Vector, ArrayList in Java
Choosing between constructor and setter methods
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments