# HashSet



![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeVrA_KrLcJnkxypAZa%2F-LeVs372rWYharIoTMD0%2FHowHashSetWorks.png?alt=media\&token=804e503f-0a40-4494-b246-9c4bd83d14e1)

```java
package com.gs.corejava.collections;

//Java program to demonstrate 
//internal working of HashSet 

import java.util.HashSet;

public class TestHashSet {
	public static void main(String args[]) {
		// creating a HashSet
		HashSet hs = new HashSet();

		// adding elements to hashset
		// using add() method
		boolean b1 = hs.add("RED");
		boolean b2 = hs.add("GREEN");
		boolean b3 = hs.add("BLUE");
		boolean b4 = hs.add("PINK");

		// REMOVE ELEMENT
		boolean b5 = hs.remove("RED");

		// adding duplicate element
		boolean b6 = hs.add("GREEN");

		// printing b1, b2, b3
		System.out.println("b1 = " + b1);
		System.out.println("b2 = " + b2);
		System.out.println("b3 = " + b3);
		System.out.println("b4 = " + b4);
		System.out.println("b5 = " + b5);
		System.out.println("b6 = " + b6);

		// printing all elements of hashset
		System.out.println(hs);

	}
}
```

```
b1 = true
b2 = true
b3 = true
b4 = true
b5 = true
b6 = false
[BLUE, GREEN, PINK]
```

### Java Code :

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeVrA_KrLcJnkxypAZa%2F-LeVtLUuxtDf4dILM21_%2FScreen-Shot-2018-12-16-at-12.05.52-PM-1.png?alt=media\&token=af6b35ab-d96f-4936-a5b8-34242b074694)

```java
// predefined HashSet class
public class HashSet
{
    // A HashMap object 
    private transient HashMap map;

    // A Dummy value(PRESENT) to associate with an Object in the Map
    private static final Object PRESENT = new Object();
    
    // default constructor of HashSet class
    // It creates a HashMap by calling 
    // default constructor of HashMap class
    public HashSet() {
        map = new HashMap<>();
    }

    // add method 
    // it calls put() method on map object
    // and then compares it's return value with null
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }
 
    // Other methods in Hash Set
}
```

Now as you can see that whenever we create a HashSet, it internally creates a HashMap and if we insert an element into this HashSet using _add()_ method, it actually call _put()_ method on internally created HashMap object with element you have specified as it’s key and constant Object called **“PRESENT”** as it’s value. So we can say that **a Set achieves uniqueness internally through HashMap**. Now the whole story comes around how a HashMap and _put()_ method internally works.

As we know in a HashMap each key is unique and when we call _put(Key, Value)_ method, it returns the previous value associated with key, or _null_ if there was no mapping for key. So in _add()_ method we check the return value of map.put(key, value) method with _null_ value.

1. If map.put(key, value) returns _null_, then the statement “map.put(e, PRESENT) == null” will return _true_ and element is added to the HashSet(internally HashMap).
2. If map.put(key, value) returns old value of the key, then the statement “map.put(e, PRESENT) == null” will return _false_ and element is not added to the HashSet(internally HashMap).

### Example :

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LeVrA_KrLcJnkxypAZa%2F-LeVtPQywPVQzJ_l2Jra%2Fhash-set-working.png?alt=media\&token=93f11f30-f5ba-4040-bf79-18dbfcf8996b)

