The Java HashSet class is used to create a collection that stores data in a hash table. It derives from AbstractSet and implements the Set interface.
This class allows for the null element. The class also provides constant time performance for fundamental operations such as add, delete, contains, and size, assuming the hash function properly distributes the elements throughout the buckets, as we will see later in the article.
Declaration of the Java HashSet class:
public class HashSet<E>extends AbstractSet<E>implements Set<E>, Cloneable, Serializable
The following are the key features of the Java HashSet class:
- HashSet uses a process known as hashing to store the elements.
- HashSet only contains unique elements.
- HashSet accepts null values.
- The HashSet class does not support synchronization.
- The insertion order is not preserved by HashSet. Elements are put here based on their hashcode.
HashSet Constructors:
- HashSet(): It is used to construct a default HashSet.
- HashSet(Collection c): It is used to populate the hash set with elements from the collection c.
- HashSet(int capacity): It is used to set the hash’s capacity to the specified integer value capacity. As elements are added to the HashSet, the capacity grows automatically.
HashSet Method:
Modifier & Type | Method | Description |
---|---|---|
boolean | add(E e) | If the supplied element does not already exist in this set, it is added. |
void | clear() | It is used to remove all of the set’s elements. |
object | clone() | It is used to return a shallow replica of this HashSet instance, with no cloning of the elements. |
boolean | contains(Object o) | It is used to return true if the supplied element is present in this set. |
boolean | isEmpty() | It returns true if this set includes no elements. |
Iterator<E> | iterator() | It is used to return an iterator across the set’s elements. |
boolean | remove(Object o) | If the specified element is present, it is removed from this set. |
int | size() | It returns the number of elements in the set. |
Spliterator<E> | spliterator() | It is used to construct a late-binding and fail-fast Spliterator across the set’s elements. |
Example: Adding Elements to HashSet
import java.io.*;
import java.util.*;
// Main class
// AddingElementsToHashSet
class Coderz {
public static void main(String[] args)
{
// Creating an empty HashSet of string entities
HashSet<String> hobj = new HashSet<String>();
// Adding elements using add() method
hobj.add("coderzpy.com");
hobj.add("For");
hobj.add("coding");
// Printing all string el=ntries inside the Set
System.out.println("HashSet elements : " + hobj);
}
}
Output:
HashSet elements : [coding, For, coderzpy.com]
Example: Removal Of Elements of HashSet
import java.io.*;
import java.util.*;
class Coderz {
// Main driver method
public static void main(String[] args)
{
// Creating an
HashSet<String> hobj = new HashSet<String>();
// Adding elements to above Set
// using add() method
hobj.add("Apple");
hobj.add("Banana");
hobj.add("Cat");
hobj.add("A");
hobj.add("B");
hobj.add("Z");
// Printing the elements of HashSet elements
System.out.println("Initial HashSet " + hobj);
// Removing the element B
hobj.remove("B");
// Printing the updated HashSet elements
System.out.println("After removing element " + hobj);
// Returns false if the element is not present
System.out.println("Element AC exists in the Set : "
+ hobj.remove("AC"));
}
}
Output:
Initial HashSet [A, Apple, B, Cat, Z, Banana]
After removing element [A, Apple, Cat, Z, Banana]
Element AC exists in the Set : false
Example: Iteration Over HashSet
import java.io.*;
import java.util.*;
class Coderz {
// Main driver method
public static void main(String[] args)
{
// Creating an empty HashSet of string entries
HashSet<String> hobj = new HashSet<String>();
// Adding elements to above Set
// using add() method
hobj.add("Coderzpy.com");
hobj.add("is fun");
hobj.add("for coding");
hobj.add("A");
hobj.add("B");
hobj.add("Z");
// Iterating though the HashSet using iterators
Iterator itr = hobj.iterator();
// Holds true till there is single element
// remaining in Set
while (itr.hasNext())
// Traversing elements and printing them
System.out.print(itr.next() + ", ");
System.out.println();
// Using enhanced for loop for traversal
for (String s : hobj)
// Traversing elements and printing them
System.out.print(s + ", ");
System.out.println();
}
}
Output:
A, B, Coderzpy.com, for coding, is fun, Z,
A, B, Coderzpy.com, for coding, is fun, Z,
Note: also read about the Map Interface in Java Collection Framework
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment