What is the BitSet.valueOf() method in Java?

valueOf() is a static method of the BitSet class that gets a BitSet object from any of the following data structures:

  • LongBuffer
  • long array
  • ByteBuffer
  • byte array

The valueOf method is defined in the BitSet class. The BitSet class is defined in the java.util package. To import the BitSet class check the following import statement:

import java.util.BitSet;

valueOf(long[] longs)

This method creates a BitSet object from a long array.

Syntax


public static BitSet valueOf(long[] longs)

Parameter(s)

  • long[] longs: The array to convert to the BitSet object.

Return value

This method returns a BitSet object.

valueOf(LongBuffer lb)

This method creates a BitSet object from the LongBuffer object.

Syntax


public static BitSet valueOf(LongBuffer lb)

Parameter(s)

  • LongBuffer lb: The buffer to convert to the BitSet object.

Return value

This method returns a BitSet object.

valueOf(byte[] bytes)

This method creates a BitSet object from a byte array.

Syntax


public static BitSet valueOf(byte[] bytes)

Parameter(s)

  • byte[] bytes: The byte array to convert to the BitSet object.

Return value

This method returns a BitSet object.

valueOf(ByteBuffer bb)

This method creates a BitSet object from a byte buffer.

Syntax


public static BitSet valueOf(ByteBuffer bb)

Parameter(s)

  • ByteBuffer bb: The byte buffer to convert to the BitSet object.

Return value

This method returns a BitSet object.

Code

import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.util.Arrays;
import java.util.BitSet;
public class Main {
private static void bitSetFromLongArray(){
long[] longs = {4, 8, 16, 32};
BitSet bitSet = BitSet.valueOf(longs);
System.out.printf("%s converted to BitSet object - %s\n", Arrays.toString(longs), bitSet);
}
private static void bitSetFromLongBuffer(){
LongBuffer longBuffer = LongBuffer.wrap(new long[] { 5, 3, 2 });
BitSet bitSet = BitSet.valueOf(longBuffer);
System.out.printf("%s converted to BitSet object - %s\n", longBuffer, bitSet);
}
private static void bitSetFromByteArray(){
byte[] bytes = new byte[]{4, 3, 1};
BitSet bitSet = BitSet.valueOf(bytes);
System.out.printf("%s converted to BitSet object - %s\n", Arrays.toString(bytes), bitSet);
}
private static void bitSetFromByteBuffer(){
ByteBuffer bb = ByteBuffer.wrap(new byte[] { 5, 3, 2 });
BitSet bitSet = BitSet.valueOf(bb);
System.out.printf("%s converted to BitSet object - %s\n", bb, bitSet);
}
public static void main(String[] args) {
bitSetFromLongArray();
bitSetFromByteArray();
bitSetFromByteBuffer();
bitSetFromLongBuffer();
}
}

Explanation

  • Lines 1 to 4: We import the relevant classes.
  • Line 9: We define a long array.
  • Line 11: We use the valueOf() method to convert the long array to the BitSet object.
  • Line 13: We print the long array and the created BitSet object.
  • Line 18: We define a LongBuffer.
  • Line 20: We use the valueOf() method to convert the LongBuffer to the BitSet object.
  • Line 22: We print the LongBuffer and the created BitSet object.
  • Line 27: We define a byte array.
  • Line 29: We use the valueOf() method to convert the byte array to the BitSet object.
  • Line 31: We print the byte array and the created BitSet object.
  • Line 35: We define a ByteBuffer.
  • Line 37: We use the valueOf() method to convert the ByteBuffer to the BitSet object.
  • Line 39: We print the ByteBuffer and the created BitSet object.

Free Resources