Java Utility Library

Java BitSet - intersects() Method



The java.util.BitSet.intersects() method is used to check whether the argument BitSet has any bits set to true that are also set to true in the given BitSet or not. The method returns true if the argument BitSet has any bits set to true which are also set to true in the given BitSet, else returns false.

Syntax

public boolean intersects(BitSet set)

Parameters

set Specify the BitSet to intersect with.

Return Value

Returns true if this BitSet intersects the specified BitSet, else returns false.

Exception

NA.

Example:

In the example below, the java.util.BitSet.intersects() method is used to check whether the given BitSet intersects with the specified BitSet or not.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating BitSets
    BitSet BSet1 = new BitSet();
    BitSet BSet2 = new BitSet();
    BitSet BSet3 = new BitSet();

    //populating BSet1
    BSet1.set(10);
    BSet1.set(20);
    BSet1.set(30);

    //populating BSet2
    BSet2.set(20);
    BSet2.set(40);
    BSet2.set(60);

    //populating BSet3
    BSet3.set(25);
    BSet3.set(50);
    BSet3.set(75);

    //checking intersection of BSet1 with BSet2
    System.out.print("BSet1 intersects with BSet2: ");
    System.out.print(BSet1.intersects(BSet2));

    //checking intersection of BSet1 with BSet3
    System.out.print("\nBSet1 intersects with BSet3: ");
    System.out.print(BSet1.intersects(BSet3));
  }
}

The output of the above code will be:

BSet1 intersects with BSet2: true
BSet1 intersects with BSet3: false

❮ Java.util - BitSet