Wednesday, July 8, 2026
HomeLanguagesJavaBitSet hashCode Method in Java with Examples

BitSet hashCode Method in Java with Examples

The hashCode() Method of BitSet class in Java is used to fetch the hash code value of a particular this BitSet. This returned hash codes value depends on the set bits of the bit set being passed.

Syntax:

BitSet.hashCode()

Parameters: The method does not accept any parameters.

Return Value: The method returns the hashcode value of the BitSet.

Below programs are used to illustrate the working of BitSet.hashCode() Method:
Program 1:




// Java code to illustrate HashCode()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(10);
        init_bitset.set(20);
        init_bitset.set(30);
        init_bitset.set(40);
        init_bitset.set(50);
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        // Displaying the hashcode
        System.out.println("The HashCode: "
                           + init_bitset.hashCode());
    }
}


Output:

BitSet: {10, 20, 30, 40, 50}
The HashCode: 1075053010

Program 2:




// Java code to illustrate HashCode()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(40);
        init_bitset.set(25);
        init_bitset.set(31);
        init_bitset.set(48);
        init_bitset.set(53);
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        // Displaying the hashcode
        System.out.println("The HashCode: "
                           + init_bitset.hashCode());
    }
}


Output:

BitSet: {25, 31, 40, 48, 53}
The HashCode: -2111765038
RELATED ARTICLES

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12016 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6977 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS