Java Utility Library

Java BitSet - clone() Method



The java.util.BitSet.clone() method is used to clone this BitSet which produces a new BitSet that is equal to it. The clone of the BitSet is another BitSet that has exactly the same bits set to true as this BitSet.

Syntax

public Object clone()

Parameters

No parameter is required.

Return Value

Returns a clone of the given BitSet.

Exception

NA

Example:

In the example below, the java.util.BitSet.clone() method is used to create a clone of the BitSet called BSet.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a BitSet
    BitSet BSet = new BitSet();

    //populating BitSet
    BSet.set(10);
    BSet.set(20);
    BSet.set(30);
    BSet.set(40);
    BSet.set(50);
    
    //cloning the given BitSet
    BitSet newBSet = new BitSet();
    newBSet = (BitSet) BSet.clone();

    //printing BitSet
    System.out.println("newBSet contains: " + newBSet);
  }
}

The output of the above code will be:

newBSet contains: {10, 20, 30, 40, 50}

❮ Java.util - BitSet