BitSetUtils { public static List<Boolean> toList(BitSet bitset, int n) { if (n < 0) { throw new IllegalArgumentException("Size of list must not be negative but was + " + n); } List<Boolean> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(bitset.get(i)); } return list; } private  BitSetUtils(); static BitSet copy(BitSet b); static BitSet getRandomBits(int n, Random random); static String toString(BitSet bits, int n); static String toString(BitSet bits); static boolean innerProduct(BitSet a, BitSet b); static BitSet fromList(List<Boolean> list); static BitSet fromArray(boolean[] array); static boolean[] toArray(BitSet bitset, int length); static List<Boolean> toList(BitSet bitset, int n);  }
@Test public void testToList() { int[] indices = new int[] { 0, 1, 4, 10, 6, 9, 11 }; BitSet bitSet = new BitSet(20); for (int i: indices) { bitSet.set(i); } List<Boolean> list = BitSetUtils.toList(bitSet, bitSet.size()); Assert.assertEquals(bitSet.size(), list.size()); for (int i = 0; i < bitSet.size(); i++) { Assert.assertEquals(bitSet.get(i), list.get(i)); } List<Boolean> shortList = BitSetUtils.toList(bitSet, 5); Assert.assertEquals(5, shortList.size()); for (int i = 0; i < 5; i++) { Assert.assertEquals(bitSet.get(i), shortList.get(i)); } List<Boolean> longList = BitSetUtils.toList(bitSet, bitSet.size() + 10); Assert.assertEquals(bitSet.size() + 10, longList.size()); for (int i = 0; i < bitSet.size() + 10; i++) { Assert.assertEquals(bitSet.get(i), longList.get(i)); } boolean exception = false; try { BitSetUtils.toList(bitSet, -2); } catch (IllegalArgumentException e) { exception = true; } Assert.assertTrue(exception); }