Hey there!! Welcome to flower brackets blog. In this post we are going to learn hashset java.
Hashset is a data structure that works on the principle of hashing. It won’t allow duplicate elements. It allows only one “null” value.
Hashset stores elements, not “key/value” pair. It internally uses hashmap for storing elements.
Also read – ArrayList java
It is not synchronized. Hashset class implements set interface and it is backed up by hash table.
Hashset class offers constant time performance for basic operations like add, remove, contains and size. It is best used for frequent search operation.
It makes no assurance as to the iteration order of the set. That is, there is no guarantee that the order will remain constant over time.
Also read – java Treeset
Because underlying data structure for hashset is hashtable. So in usual case time complexity for basic operations like add, remove, contains and size hashset takes big “O” of “one” time, that is, O(1) time.
Initial capacity : is initial number of buckets that a Hashset object can hold.
Load factor : is a measure to calculate till what load HashSet can allow elements to insert before size is increased.
no. of elements in the table
load factor = ————————————————-
size of hash table
NOTE : best load factor would be 0.75 with respect to time and space complexity, according to java.
Here you can see hierarchy of hashset class. Hashset class extends abstractset class. AbstractSet class implements set interface.
And set interface extends collection interface. Collection interface extends iterable interface.
java.util Class HashSet<E> java.lang.Object java.util.AbstractCollection<E> java.util.AbstractSet<E> java.util.HashSet<E> Parameters E - type of elements maintained by this set. Implemented Interfaces Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E> Subclasses JobStateReasons, LinkedHashSet
Class that hashset class belongs to is java.util package. Here letter “E” is type of elements which we can add it to the hashset.
The type can be string or integer or employee object or user object etc. and hashset which extends abstractset.
Also read – sort Hashmap by value
Abstractset extends abstractcollection and abstractcollection extends object.
Next you can see some of the implemented interfaces like serializable, cloneable, iterable, collection and set.
Moving on subclasses of hashset are jobstatereasons and linkedhashset.
Declaration – hashset class
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
|boolean add ( E e )||adds the specified element to this set if it is not already present|
|void clear ( )||removes all of the elements from this set.|
|Object clone ( )||returns a shallow copy of this HashSet instance: the elements themselves are not cloned.|
|boolean contains ( Object o )||returns true if this set contains the specified element.|
|boolean isEmpty ( )||method used to check whether the set is empty or not. Returns true if this set contains no elements and false for non-empty condition.|
|Iterator iterator()||returns an iterator over the elements in this set.|
|boolean remove ( Object o )||removes the specified element from this set if it is present.|
|int size ( )||returns the number of elements in this set ( its cardinality ).|
Below are some of the methods inherited
Methods inherited from class java.util.AbstractSet removeAll, equals, hashCode Methods inherited from class java.util.AbstractCollection retainAll, addAll, containsAll, toArray, toString, toArray Methods inherited from class java.lang.Object wait, notifyAll, notify, finalize, getClass, wait, wait Methods inherited from interface java.util.Set toArray, retainAll, removeAll, hashCode, equals, containsAll, addAll, toArray
Constructors in hashset
Here we are going to discuss hashset constructor. Below is the first default constructor,
HashSet h = new HashSet();
Using above constructor we can construct new empty HashSet with the initial capacity 16 and load factor 0.75
This is the second HashSet constructor which accepts initialCapacity,
HashSet h = new HashSet(int initialCapacity);
Suppose say initial capacity is 5 then above constructor will construct empty HashSet with initial capacity 5 and default load factor 0.75.
Next comes the third HashSet constructor which accepts initialCapacity and loadFactor,
HashSet h = new HashSet(int initialCapacity, float loadFactor);
Suppose say initial capacity is 5 and load factor is 1.25 then above constructor will create empty HashSet with initial capacity 5 and load factor 1.25.
Also read – random number generator java
The final HashSet constructor is as follows. This HashSet constructor accepts Collection (Collection can be ArrayList or LinkedList or Set).
HashSet h = new HashSet(Collection C);
Suppose say in Collection there are three objects and if we pass Collection to above HashSet constructor what happens is above HashSet constructor will create a new HashSet and it will contain whatever elements present in the Collection.
Let’s see java program on hashset,
public class HashsetExample
public static void main(String args)
// HashSet declaration
HashSet<String> hs = new HashSet<String>();
// adding elements
// now adding duplicate values to HashSet
// display the HashSet
System.out.println("HashSet contains orange or not : " + hs.contains("orange"));
// removing item from HashSet using remove() method
System.out.println("HashSet after removing mango : " + hs);
// iterate over hashset
System.out.println("Iterating over list : ");
Iterator<String> iterate = hs.iterator();
[orange, strawberry, mango]
HashSet contains orange or not : true
HashSet after removing mango : [orange, strawberry]
Iterating over list :
Reference – Oracle help centre