简体   繁体   中英

Encode an array of long values as a BitSet

I saw that we can encode an array of ints into a BitSet and retrieve them as in:

int[] ints = new int[]{5, 7, 25, 100102244};

BitSet b = new BitSet();
for (int i=0;i<ints.length;i++)
    b.set(ints[i]);
b.stream().forEach(i -> System.out.println(i));

which outputs

5
7
25
100102244

Is it possible to do the same for an array of longs?

Thanks in advance,

Use LongBuffer :

import java.nio.*;
import java.util.*;

var buffer = LongBuffer.wrap(new long[] { 5, 7, 25, 100102244 });
var bitset = BitSet.valueOf(buffer);

System.out.println(bitset);

for (long l : bitset.toLongArray()) {
    System.out.println(l);
}
{0, 2, 64, 65, 66, 128, 131, 132, 194, 197, 198, 204, 205, 206, 208, 209, 210, 212, 213, 214, 215, 216, 218}
5
7
25
100102244

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM