Saturday, November 22, 2025
HomeLanguagesJavaJava.util.BitSet.get() in Java

Java.util.BitSet.get() in Java

There are two variants of get() in Bitset, both are discussed in this article. 1. boolean get( int value ) : Returns true if the value is present in Bitset, else returns false.

Declaration : 
public boolean get(int value)
Parameters : 
value : The value to check.
Return Value : 
Returns boolean true, if element present else returns false.

Java




// Java code to demonstrate the
// working of get() in Bitset
 
import java.util.*;
 
public class BitGet1 {
 
public static void main(String[] args)
    {
 
        // declaring bitset
        BitSet bset = new BitSet(5);
 
        // adding values using set()
        bset.set(0);
        bset.set(1);
        bset.set(2);
        bset.set(4);
 
        // checking if 3 is in BitSet
        System.out.println("Does 3 exist in Bitset? : " + bset.get(3));
 
        // checking if 4 is in BitSet
        System.out.println("Does 4 exist in Bitset? : " + bset.get(4));
    }
}


Output :

Does 3 exist in Bitset? : false
Does 4 exist in Bitset? : true

2. Bitset get(int fromval, int toval) : method returns a new BitSet composed of elements present in Bitset from fromval (inclusive) to toval (exclusive).

Declaration : 
public BitSet get(int fromval, int toval)
Parameters : 

fromval :  first value to include.
toval : last value to include(ex).

Return Value
This method returns a new BitSet from a range of this BitSet.

Java




// Java code to demonstrate the
// working of get(int fromval, int toval)
// in Bitset
 
import java.util.*;
 
public class BitGet2 {
 
public static void main(String[] args)
    {
 
        // declaring bitset
        BitSet bset = new BitSet(5);
 
        // adding values using set()
        bset.set(0);
        bset.set(1);
        bset.set(2);
        bset.set(3);
 
        // Printing values in range 0-2
        System.out.println("Values in BitSet from 0-2 are : " + bset.get(0, 3));
    }
}


Output:

Values in BitSet from 0-2 are : {0, 1, 2}

This article is contributed by Astha Tyagi. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS