Java BitSet | cardinality() Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report BitSet is a class defined in the java.util package. It creates an array of bits represented by 0s and 1s. Syntax public int cardinality() Explanation: The method returns the number of 1s in this BitSet. Examples: Input : set1 : {1, 2, 4} set2 : {} Output : 3 0 Java // Java program illustrating Bitset Class functions. 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 logical AND // on bs2 set with bs1 System.out.println(bs1.cardinality()); System.out.println(bs2.cardinality()); System.out.println(bs3.cardinality()); } } Output: bs1 : {0, 1, 2, 4} bs2 : {1, 2, 3, 4, 5, 6} bs3 : {} 4 6 0 Related Articles : Bitset Class Examples Set 1Bitset Class Examples Set 2Bitset Class Examples Set 3 Comment More infoAdvertise with us Next Article AbstractSet contains() method in Java with Example B barykrg Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-BitSet +1 More Practice Tags : JavaMisc Similar Reads Java BitSet | intersects() BitSet is a class defined in the java.util package. It creates an array of bits represented by 0s and 1s. Syntax public boolean intersects(BitSet set) Parameter: The method accepts a mandatory parameter set, which is the BitSet to be checked for intersection. Returns: The method returns a boolean in 2 min read AbstractSet Class in Java In Java, the AbstractSet class is part of the Java Collections Framework. It provides a Skeleton implementation of the set interface, which is a collection that does not allow duplicate elements. This class is abstract, meaning it cannot be instantiated directly, but it can be extended to create a c 8 min read Java BitSet | or() Prerequisite : Java BitSet | Set 1BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. It performs a logical OR of the caller BitSet with the called BitSet. This BitSet gets set only when it is true and the BitSet argument has the value true. 2 min read Initializing HashSet in Java Set in Java is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored. Basically, set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation). Set has various methods to add, remove clear, size, etc to enhance th 2 min read AbstractSet contains() method in Java with Example The contains() method of Java AbstractSet is used to check whether an element is present in a set or not. It takes the element as a parameter and returns True if the element is present in the set. Syntax: public boolean contains(Object element) Parameters: The parameter element is of type set. This 2 min read ConcurrentSkipListSet in Java In Java, the ConcurrentSkipListSet is the part of the java.util.concurrent package and provides a scalable, thread-safe alternative to TreeSet. It is a sorted set that lets multiple threads safely access and modify the set at the same time without causing issues.It is thread-safe.Elements are in sor 7 min read Like