Thursday, July 4, 2024
HomeLanguagesJavaBitSet isEmpty() method in Java

BitSet isEmpty() method in Java

BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. 

Prerequisite : Java BitSet | Set 1 Bitset.isEmpty() This method is used to check if there are any bits that are set to true, in the specified BitSet. This method returns true if this BitSet contains no bits that are set to true. 

Syntax:

public boolean isEmpty()

Return Value: This method returns true if this BitSet contains no bits that are set to true. Otherwise it returns false. Below program illustrate the isEmpty() method BitSet: 

Java




// Java program illustrating Bitset
// isEmpty() function.
 
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // Constructors of BitSet class
        BitSet bs1 = new BitSet();
        BitSet bs2 = new BitSet();
        BitSet bs3 = new BitSet();
 
        /* set is BitSet class method
        explained in next articles */
        bs1.set(0);
        bs1.set(1);
        bs1.set(2);
        bs1.set(4);
 
        // assign values to bs2
        bs2.set(4);
        bs2.set(6);
        bs2.set(5);
        bs2.set(1);
        bs2.set(2);
        bs2.set(3);
 
        // Printing the 2 Bitsets
        System.out.println("bs1 : " + bs1);
        System.out.println("bs2 : " + bs2);
        System.out.println("bs3 : " + bs3);
 
        // Performing isEmpty on bitsets
        System.out.println("Is bs1 empty : "
                           + bs1.isEmpty());
        System.out.println("Is bs2 empty : "
                           + bs2.isEmpty());
        System.out.println("Is bs3 empty : "
                           + bs3.isEmpty());
    }
}


Output:

bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6}
bs3 : {}
Is bs1 empty : false
Is bs2 empty : false
Is bs3 empty : true

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments