Java BitSet intersects() method

The intersects(BitSet set) method of Java BitSet class returns Boolean value true or false on the basis of whether parameter BitSet has intersected with this BitSet or not. It returns true if the specified BitSet set is also true in this BitSet.

Syntax:

Parameter:

DataTypeParameterDescription
BitSetsetIt is a BitSet set which is to intersect.

Returns:

It returns a Boolean value true or false on the basis of whether parameter BitSet has intersected with this BitSet or not.

Exception:

NullPointerException - If null parameter will pass in the method.

Compatibility Version:

Java 1.4 and above

Example 1

Output:

bitset1: {1, 2, 3, 4}
bitset2: {1, 2, 6, 7}
bitset3: {5, 6, 7, 8}
intersected result between bitset1 and bitset2: true
intersected result between bitset1 and bitset3: false

Example 2

This method throws a NullPointerException if we pass a null parameter.

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.BitSet.intersects(Unknown Source)
	at BitSetEntersectsExample2.main(BitSetEntersectsExample2.java:14)
bitset1: {1, 2, 3, 4}