< Prev
Next >

HashSet Class

HashSet is a collection class that implements Set Interface. HashSet stores its element by a process called hashing. In hashing, hash code of every element is computed internally and the value of hash code determines the index at which the element is stored in HashSet. Hence, the order of elements in HashSet is unknown and cannot be a guaranteed.

Important points about HashSet Collection class.

Constructors :

1) HashSet()
This constructor creates an empty HashSet of a particular object type.
Example -
HashSet<Integer> arrL= new HashSet<Integer>();

This constructor example creates an empty HashSet to hold Integer objects.

2) HashSet(Collection <? extends E>c)
This constructor example creates a HashSet initialized with the elements of Collection c. With a condition, that, Collection c holds similar type of objects as the type declared by the HashSet.
Example -
HashSet<Integer> arrL1 = new HashSet<Integer>():
HashSet<Integer> arrL2 = new HashSet<Integer>(arrL1);

This constructor creates a HashSet arrL2 initialized with the elements of a HashSet arrL1. Both HashSet hold Integer objects.

Some important methods of HashSet class.

Methods Description
boolean add(E e) This method adds the specified element e to the end of the HashSet.
boolean addAll(Collection<? extends E> c) Adds the content of a Collection c to the end of the HashSet.
int size() Returns the total number of elements in the HashSet.
void clear() Removes all of the elements from the HashSet.
boolean isEmpty() Returns true is HashSet is empty, else false.
contains(Object o) Checks if HashSet contains a specific Object.
boolean remove(Object o) Removes the first occurrence of an Object from the HashSet, if it is present.
Object[] toArray() Returns an Object array containining all the elements of HashSet.
Iterator iterator() Returns an Iterator to iterate over elements of the HashSet.

Creating a HashSet and displaying its features.

In this program we have created an HashSet to hold <String> objects and we will add/remove some elements from this set and use it to initialize a new HashSet.
import java.util.*;

class HashSet1
public static void main(String... ar)
HashSet<String> hash= new HashSet<String>();


System.out.println("HashSet after adding objects = " + hash);
System.out.println("Size of HashSet = "+ hash.size());

System.out.println("After removing element G, HashSet = "+ hash);

//Creating new Hashset from existing Hashset
HashSet<String> hash2= new HashSet<String>(hash);

System.out.println("New HashSet created from an existing HashSet = " + hash2);


Output is - :

HashSet after adding objects = [1, B, 3, C, G]
Size of HashSet = 5
Afer removing G, HashSet contains = [1, B, 3, C]
New HashSet created from an existing HashSet = [1, B, 3, C]

Program Analysis

Method addAll() to add a HashSet at end of another HashSet.

import java.util.*;

class HashSet1
public static void main(String... ar)
//Creating the first HashSet 
HashSet<Integer> Hash1 = new HashSet<Integer>();


System.out.println("Contens of first HashSet= " + Hash1);
System.out.println("Size of first HashSet = "+ Hash1.size());

//Creating the second HashSet 
HashSet<Integer> Hash2= new HashSet<Integer>();
System.out.println("Contents of second HashSet : " + Hash2);

//Adding the contents of first HashSet to the end of second HashSet
System.out.println("Contents of second HashSet after adding first HashSet to its end : " + Hash2);
System.out.println("Size of second  HashSet = "+ Hash2.size());


Output is - :

Contens of first HashSet= [1, 2, 3, 4, 5]
Size of first HashSet = 5
Contents of second HashSet : [200, 300]
Contents of second HashSet after adding first HashSet to its end : [1, 2, 3, 4,5, 200, 300]
Size of second  HashSet = 7

Method add() is called to add the elements of an existing HashSet(Hash1) to the end of elements of another HashSet(Hash2).

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

<< Prev
Next >
<< LinkedList Class
LinkedHashSet Class >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next