Java BitSet clone() Method



Description

The Java BitSet clone() method produces a new BitSet that is equal to this one. The clone of the bit set is another bit set that has exactly the same bits set to true as this bit set.

Declaration

Following is the declaration for java.util.BitSet.clone() method

public Object clone()

Parameters

NA

Return Value

This method returns a clone of this bit set.

Exception

NA

Example 1

The following example shows the usage of Java BitSet clone() method. We're creating a BitSet. We're setting true values in the BitSet object using set() method call and using clone() method we're creating a clone of first bitset and then print the new bitset.

package com.tutorialspoint;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create a bitset
      BitSet bitset = new BitSet();

      // assign values to bitset1
      bitset.set(0, 6, true);

      // print the set
      System.out.println("Bitset: " + bitset);

      // clone the bitset
      Object bitset1 = bitset.clone();

      // print bitset1
      System.out.println("Bitset1: " + bitset1);
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset: {0, 1, 2, 3, 4, 5}
Bitset1: {0, 1, 2, 3, 4, 5}

Example 2

The following example shows the usage of Java BitSet clone() method. We're creating two BitSets using byte[] and using clone() method we're creating a clone of first bitset and then print the new bitset.

package com.tutorialspoint;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create a bitset
      BitSet bitset = BitSet.valueOf(new byte[] { 0, 1, 2, 3, 4, 5 });

      // print the set
      System.out.println("Bitset: " + bitset);

      // clone the bitset
      Object bitset1 = bitset.clone();

      // print bitset1
      System.out.println("Bitset1: " + bitset1);
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset: {8, 17, 24, 25, 34, 40, 42}
Bitset1: {8, 17, 24, 25, 34, 40, 42}

Example 3

The following example shows the usage of Java BitSet clone() method. We're creating two BitSets using long[] and using clone() method we're creating a clone of first bitset and then print the new bitset.

package com.tutorialspoint;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create a bitset
      BitSet bitset = BitSet.valueOf(new long[] { 0, 1, 2, 3, 4, 5 });

      // print the set
      System.out.println("Bitset: " + bitset);

      // clone the bitset
      Object bitset1 = bitset.clone();

      // print bitset1
      System.out.println("Bitset1: " + bitset1);
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset: {64, 129, 192, 193, 258, 320, 322}
Bitset1: {64, 129, 192, 193, 258, 320, 322}
java_util_bitset.htm
Advertisements