java.util.BitSet — set() doesn't work as expected

后端 未结 6 1357
借酒劲吻你
借酒劲吻你 2021-01-18 01:17

Am I missing something painfully obvious? Or does just nobody in the world actually use java.util.BitSet?

The following test fails:

@Test
public voi         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-18 01:37

    // Abhay Dandekar

    import java.util.BitSet;
    
    public class TestBitSet {
    
        public static void main(String[] args) {
    
            BitSet bitSet = new BitSet();
            System.out.println("State 0 : " + bitSet.size() + " : " + bitSet.length() );
    
            bitSet.set(0, true);
            bitSet.set(1, true);
            System.out.println("State 1 : " + bitSet.size() + " : " + bitSet.length() );
    
            bitSet.set(2, false);
            bitSet.set(3, false);
            System.out.println("State 2 : " + bitSet.size() + " : " + bitSet.length() );
    
            bitSet.set(4, true);
            System.out.println("State 3 : " + bitSet.size() + " : " + bitSet.length() );
    
        }
    }
    

    A simple java program to show what happens inside. Some points to note :

    1. BitSet is backed by a long

    2. All the default values are false

    3. While returning the length, it returns the index+1 of the highest "true" value in the set.

    The output below should be able to explain itself :

    State 0 : 64 : 0
    
    State 1 : 64 : 2
    
    State 2 : 64 : 2
    
    State 3 : 64 : 5
    

    So points to conclude :

    1. Do not use the length to conclude the no of bits modified

    2. Can be used in scenarios like bloom filters. More on bloom filters can be googled .. ;)

    Hope this helps

    Regards,

    Abhay Dandekar

提交回复
热议问题