What is the reason for BitSet's size() method?

后端 未结 4 1326
青春惊慌失措
青春惊慌失措 2021-02-07 12:06

Is there a use case for the size() method on the java.util.BitSet class?

I mean - the JavaDoc clearly says it\'s implementation dependant, it returns the size of the int

4条回答
  •  囚心锁ツ
    2021-02-07 12:53

    One of the main reason i think it may be useful is when we need to extend the BitSet class and override the length method. In that case, the size is useful. below is how length returns value with dependancy on size method.

    protected Set bitset;
    public int length() {
      int returnValue = 0;
      // Make sure set not empty
      // Get maximum value +1
      if (bitset.size() > 0) {
         Integer max = (Integer)Collections.max(bitset);
         returnValue = max.intValue()+1;
      }
      return returnValue;
     }
    

提交回复
热议问题