An IDEA Live Template for iterating through a BitSet
# 2005-08-30 22:10:48 -0400 | General / Java | 1 CommentI love
IDEA’s Live Templates, and I often find myself using
java.util.BitSet.
The BitSet javadoc
describes
a neat way of iterating through the 1 bits, and I do this often enough that it warranted a custom Live Template.
Now all I need to do is hit itbs<TAB> to have this code automagically appear.
The template looks like this:
for(int $INDEX$ = $BS$.nextSetBit(0); $INDEX$ >= 0; $INDEX$ = $BS$.nextSetBit($INDEX$ + 1)) {
$END$
}
I’ve captured a few screenshots since you also have to setup variables for $INDEX$ and $BS$.

One minor caveat worth noting: BitSet.nextSetBit() and BitSet.nextClearBit() were introduced with JDK1.4, so if you need your code to run on a pre-1.4 VM, you can’t use this. :-(