Java HashSet

The Hashset class extends the AbstractSet class. The HashSet class implements the Set interface. This class deals with a collection, whose elements are stored in a hash table. The hashing mechanism is used to store the elements in a hash table. This mechanism uses the informational content of a key to determine a unique value. Such a unique value is known as a hash code. The hash code is then used as the index. The data associated with the key is stored at this index.

The important forms of the constructor of the hashSet class are as follows:

HashSet ( )
HashSet (Collection c)

When we use the first form of the constructor, a default hash set will be created. The second form of the constructor initializes the elements of the newly created hash set by using the elements of the collection c.

Progarm

0

 

 

 

Program Source

import java.util.HashSet;

public class Javaapp {

    public static void main(String[] args) {
        
        HashSet hs = new HashSet();
        hs.add(20);
        hs.add(20);
        hs.add(40);
        hs.add(40);
        hs.add(60);
        hs.add(60);
        hs.add(80);
        hs.add(80);
        System.out.println(hs);
    }
}

Leave a Comment