bitmap本质使用一个bit来表示一个元素,在数据量比较大的情况下,数据去重,数据排序,数据查找,使用这种数据架构有这很大的空间优势。
在实现上,Java中最小单位是byte, 1byte=8bit, 所以如果使用Java来实现bitmap, 就要用到位移、或运算,这里使用Java的bitset
package org.example; import java.util.BitSet; public class App { public static void main(String[] args) { BitSet bitSet = new BitSet(10); bitSet.set(1, true); bitSet.set(2, true); bitSet.set(3, true); System.out.println(bitSet.length()); bitSet.set(11, true); System.out.println(bitSet.length()); System.out.println(bitSet.get(10)); System.out.println(bitSet.get(11)); } }