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:
LongBufferlongarrayByteBufferbytearray
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 theBitSetobject.
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 theBitSetobject.
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 theBitSetobject.
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 theBitSetobject.
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
longarray. - Line 11: We use the
valueOf()method to convert thelongarray to theBitSetobject. - Line 13: We print the
longarray and the createdBitSetobject. - Line 18: We define a
LongBuffer. - Line 20: We use the
valueOf()method to convert theLongBufferto theBitSetobject. - Line 22: We print the
LongBufferand the createdBitSetobject. - Line 27: We define a
bytearray. - Line 29: We use the
valueOf()method to convert thebytearray to theBitSetobject. - Line 31: We print the
bytearray and the createdBitSetobject. - Line 35: We define a
ByteBuffer. - Line 37: We use the
valueOf()method to convert theByteBufferto theBitSetobject. - Line 39: We print the
ByteBufferand the createdBitSetobject.