Java HashSet With Example

HashSet is the implemenation class of the Set interface.It Follows HashTable data structure to store objects.

A HashSet is an unsorted, unordered Set.

HashSet doesn't contain any duplicate elements.which means HashSet allows only unique elements.If We add any duplicate elements we wot get any compile time error. just add method returns false.

HashSet uses the hashcode of the object being inserted.So Here point is HashSet is doest allow any insertion order.insertion order based on the hashcode of the object beig inserted.

HashSet allow even null value also for only once.

HashSet is not Synchroized.

See the internal implementation of HashSet


public class HashSet
extends AbstractSet
implements Set, Cloneable, java.io.Serializable
{
static final long serialVersionUID = -5024744406713321676L;

private transient HashMap map;

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();

/**
* Constructs a new, empty set; the backing HashMap instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}


public HashSet(Collection c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}


public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}




HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}

//more code goes here

}

Constructors of Java HashSet


HashSet hashset = new HashSet();

The Above line Constructs a new, empty HashSet istance; the backing HashMap instance has default initial capacity (16) and load factor (0.75).

HashSet hashset = new HashSet(Collection c);

The Above line Constructs a new Hashset instance containing the elements in the specified collection.

HashSet hashset = new HashSet(int initialCapacity);

The Above line Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).

Now Lets See methods of Java HashSet


public boolean add(E e)

The Above method Adds the specified element to the HashSet if it is not already present.we can call add method on HashSet like below.


HashSet hashSet = new HashSet();
hashset.add("AAA");
hashSet.add("BBB");

public boolean remove(Object o)

The above method Removes the specified element from this HashSet if element is already present in Hashset.we can call remove method on HashSet like below.


HashSet hashSet = new HashSet();
hashset.remove("AAA");

public void clear()

The Above Method Removes all of the elements from the Hashset. The HashSet will be empty after this call returns.we can call clear method on HashSet like below.


HashSet hashSet = new HashSet();
hashset.clear();

public Object clone()

The Above Method Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.


Example To Java Hashset


package com.java2study;

import java.util.HashSet;

public class HashSetDemo{


public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add("AAAA");
hashSet.add("BBBB");
hashSet.add("CCCC");
hashSet.add("DDDD");
System.out.println("The HashSet elements:"+hashSet);

hashSet.remove("AAAA");

System.out.println("HashSet After Removig specified element:"+hashSet);

Object o = hashSet.clone();
System.out.println("the hashset clone"+o);

hashSet.clear();
System.out.println(hashSet);
}



}

output


The HashSet elements:[DDDD, AAAA, BBBB, CCCC]
HashSet After Removig specified element:[DDDD, BBBB, CCCC]
the hashset clone[BBBB, DDDD, CCCC]
[]

No comments:

Post a Comment