Menu
Topics Index
...
`


Collections Framework > Collection Classes >
Siva Nookala - 14 Apr 2016
HashSet extends AbstractSet and implements the Set Interface In Java.

Declaration of HashSet:
class HashSet< E >
Here, E specifies the type of objects that the set holds.

In HashSet hash table is used for storage. A hash table stores information by using a mechanism called hashing. In hashing, the information is stored in the form of (key, value). Here key is used to determine a unique value, called hashcode. The hashcode is then used as the index to retrieve the data associated with the key.
Point to remember in HashSet:
  • HashSet doesn't allow duplicates.
  • The transformation of the key into its hash code is performed automatically and we never see the hash code itself.
  • We can't directly index the hash table without hash code.
  • HashSet does not define any additional methods other than the methods of its superclasses and interfaces.
  • HashSet does not guarantee the order of its elements.
The following are the constructors defined by HahsSet.
HashSet()
Creats a default hash set.

HashSet(Collection<? extends E> c)
Creats a hash set by using the elements of c.

HashSet(int capacity)
Creats a hash set and initializes the capacity to capacity.(The default capacity is 16)

HashSet(int capacity, float fillRatio)
Creats a hash set and initializes both the capacity and fill ratio(also called load capacity) of the hash set from its arguments. The fill ratio must be between 0.0 and 1.0. When the number of elements is greater than the capacity, the capacity of the hash set is multiplied with the fill ratio and the hash set is expanded.

HashSetDemo
import java.util.*;

class HashSetDemo
{
    public static void main(String arg[])
    {
        List<Character> characters = new ArrayList<Character>(); //
        characters.add('A');
        characters.add('B');
        characters.add('C');
        characters.add('D');
        characters.add('2');
        HashSet<Character> hashcharacters = new HashSet<Character>(characters); // LINE B
        System.out.println("Elements in hashcharacters: " + hashcharacters); // LINE C
        System.out.print("retrieving in order: "); // LINE D
        for (int i = 0; i < hashcharacters.size(); i++)
        {
            System.out.print(characters.get(i) + " "); // LINE E
        }
        hashcharacters.remove('2'); // LINE F
        System.out.println();
        System.out.print("Modified HashSet: " + hashcharacters); // LINE G    
    }
}
OUTPUT

Elements in hashcharacters: [D, 2, A, B, C]
retrieving in order: A B C D 2
Modified HashSet: [D, A, B, C]

DESCRIPTION

In the above program at LINE A we have created an ArrayList characters and added elements. At LINE B we have created a HashSet hashcharacters and added all the elements of characters to hashcharacters. At LINE C we are printing the elements in the hashcharacters. We can see the output as we discussed earlier the HashSet doesn't return the values in order when we call the HasSet. At LINE D we are retrieving the elements in hashcharcters in order. At LINE E inside for loop we are printing the elements in hashcharcters in order. At LINE F we have removed the element 2 from hashcharacters. At LINE G we are displaying the modified hashcharcters.

THINGS TO TRY
  • Create a HahsSet with the default constructor and add all the elements of characters.
  • At LINE F remove the single quotes for 2 and see the output.
    The program doesn't show any compilation error since the parameter to be passed for the remove method is integer and method checks for the value in the set if found it removes the object if not the HashSet will remain unchanged.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.