BitSet int hashCode() Method Example Program


Returns the hash code value for this bit set. The hash code depends only on which bits are set within this BitSet.
The hash code is defined to be the result of the following calculation:

 public int hashCode() {
     long h = 1234;
     long[] words = toLongArray();
     for (int i = words.length; --i >= 0; )
         h ^= words[i] * (i + 1);
     return (int)((h >> 32) ^ h);
 }
Note that the hash code changes if the set of bits is altered.

Program

package com.candidjava;

import java.util.BitSet;

/**
 * @author :vinod kumar v
 * @description :The BitSet.hashcode() method returns a hash code value for this
 *              bit set. The hash code depends only on which bits have been set
 *              within this BitSet.
 * */

public final class BitSetHashCode {
	public static void main(String[] args) {

		BitSet obj = new BitSet(8);
		BitSet obj1 = new BitSet(8);

		obj.set(0);
		obj.set(1);
		obj.set(2);
		obj.set(3);

		obj1.set(2);
		obj1.set(4);
		obj1.set(6);
		obj1.set(8);

		System.out.println("The Value in obj:" + obj);
		System.out.println("The Value in obj1" + obj1);

		System.out.println("" + obj.hashCode());

		System.out.println("" + obj1.hashCode());

	}
}

Output

The Value in obj:{0, 1, 2, 3}
The Value in obj1{2, 4, 6, 8}
1245
1414

Explanation

public int hashCode()
Returns the hash code value for this bit set. The hash code depends only on which bits are set within this BitSet.
The hash code is defined to be the result of the following calculation:

 public int hashCode() {
     long h = 1234;
     long[] words = toLongArray();
     for (int i = words.length; --i >= 0; )
         h ^= words[i] * (i + 1);
     return (int)((h >> 32) ^ h);
 }
Note that the hash code changes if the set of bits is altered.
Overrides:
hashCode in class Object
Returns:
the hash code value for this bit set


Related Post

Comments


©candidjava.com