本文主要介绍BitMap的算法思想,以及开源工具类JavaEWAH、RoaringBitmap的简单用法。

一、BitMap

介绍

BitMap使用bit位,来标记元素对应的Value。该算法能够节省存储空间。

假设一个场景,要存0-7以内的数字[3,5,6,1,2],尽可能的节省空间。 一种思路就是单纯使用数组存储,但如果数据量放大百万倍甚至千万倍呢,数组的所占用的内存会非常大。 另一种思路是使用BitMap。

表示[3,5,7,1,2],我们可以用8bit的空间来存储,每个数字都在对应的位置中以1的方式表示。

位置7

位置 6

位置 5

位置 4

位置 3

位置 2

位置 1

位置 0

1

0

1

0

1

1

1

0

若将上述BitMap看作是存储用户的标签,如信用卡逾期标签,位置看成用户ID,则若需要查询哪些用户有信用卡逾期的行为(标签),就非常容易查询统计了。

二、RoaringBitmap

文档中怎么说?

Bitsets, also called bitmaps, are commonly used as fast data structures. Unfortunately, they can use too much memory. To compensate, we often use compressed bitmaps.

BitMap通常被用作快速查询的数据结构,但它太占内存了。解决方案是,对BitMap进行压缩。

Roaring bitmaps are compressed bitmaps which tend to outperform conventional compressed bitmaps such as WAH, EWAH or Concise. In some instances, roaring bitmaps can be hundreds of times faster and they often offer significantly better compression. They can even be faster than uncompressed bitmaps.

Roaring bitmaps是一种超常规的压缩BitMap。它的速度比未压缩的BitMap快上百倍。

简单使用

引入依赖

org.roaringbitmap

RoaringBitmap

0.8.1

测试代码

@SpringBootTest

@RunWith(SpringRunner.class)

public class TestRoaringbitmap {

@Test

public void test(){

//向rr中添加1、2、3、1000四个数字

RoaringBitmap rr = RoaringBitmap.bitmapOf(1,2,3,1000);

//创建RoaringBitmap rr2

RoaringBitmap rr2 = new RoaringBitmap();

//向rr2中添加10000-12000共2000个数字

rr2.add(10000L,12000L);

//返回第3个数字是1000,第0个数字是1,第1个数字是2,则第3个数字是1000

rr.select(3);

//返回value = 2 时的索引为 1。value = 1 时,索引是 0 ,value=3的索引为2

rr.rank(2);

//判断是否包含1000

rr.contains(1000); // will return true

//判断是否包含7

rr.contains(7); // will return false

//两个RoaringBitmap进行or操作,数值进行合并,合并后产生新的RoaringBitmap叫rror

RoaringBitmap rror = RoaringBitmap.or(rr, rr2);

//rr与rr2进行位运算,并将值赋值给rr

rr.or(rr2);

//判断rror与rr是否相等,显然是相等的

boolean equals = rror.equals(rr);

if(!equals) throw new RuntimeException("bug");

// 查看rr中存储了多少个值,1,2,3,1000和10000-12000,共2004个数字

long cardinality = rr.getLongCardinality();

System.out.println(cardinality);

//遍历rr中的value

for(int i : rr) {

System.out.println(i);

}

//这种方式的遍历比上面的方式更快

rr.forEach((Consumer super Integer>) i -> {

System.out.println(i.intValue());

});

}

}

三、JavaEWAH

引入依赖

com.googlecode.javaewah

JavaEWAH

1.1.6

测试代码

@SpringBootTest

@RunWith(SpringRunner.class)

public class TestJavaEWAH {

@Test

public void test(){

EWAHCompressedBitmap ewahBitmap1 = EWAHCompressedBitmap.bitmapOf(0, 2, 55, 64, 1 << 30);

EWAHCompressedBitmap ewahBitmap2 = EWAHCompressedBitmap.bitmapOf(1, 3, 64,1 << 30);

//bitmap 1: {0,2,55,64,1073741824}

System.out.println("bitmap 1: " + ewahBitmap1);

//bitmap 2: {1,3,64,1073741824}

System.out.println("bitmap 2: " + ewahBitmap2);

//是否包含value=64,返回为true

System.out.println(ewahBitmap1.get(64));

//获取value的个数,个数为5

System.out.println(ewahBitmap1.cardinality());

//遍历所有value

ewahBitmap1.forEach(integer -> {

System.out.println(integer);

});

//进行位或运算

EWAHCompressedBitmap orbitmap = ewahBitmap1.or(ewahBitmap2);

//返回bitmap 1 OR bitmap 2: {0,1,2,3,55,64,1073741824}

System.out.println("bitmap 1 OR bitmap 2: " + orbitmap);

//memory usage: 40 bytes

System.out.println("memory usage: " + orbitmap.sizeInBytes() + " bytes");

//进行位与运算

EWAHCompressedBitmap andbitmap = ewahBitmap1.and(ewahBitmap2);

//返回bitmap 1 AND bitmap 2: {64,1073741824}

System.out.println("bitmap 1 AND bitmap 2: " + andbitmap);

//memory usage: 32 bytes

System.out.println("memory usage: " + andbitmap.sizeInBytes() + " bytes");

//序列化与反序列化

try {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ewahBitmap1.serialize(new DataOutputStream(bos));

EWAHCompressedBitmap ewahBitmap1new = new EWAHCompressedBitmap();

byte[] bout = bos.toByteArray();

ewahBitmap1new.deserialize(new DataInputStream(new ByteArrayInputStream(bout)));

System.out.println("bitmap 1 (recovered) : " + ewahBitmap1new);

} catch (IOException e) {

e.printStackTrace();

}

}

}

参考文档

[1]: BitMap算法详解 [2]: 漫画:Bitmap算法 整合版 [3]: RoaringBitmap GitHub项目文档 [4]: JavaEWAH GitHub项目文档

原文出处:https://www.cnblogs.com/fonxian/p/10937882.html

roaringbitmap java,BitMap、RoaringBitmap与JavaEWAH相关推荐

  1. roaringbitmap java,roaringbitmap 源码解析 bitmap add过程

    最近在做标签平台的分析引擎.底层涉及到位图的处理,所以涉及到roaringbitmap.

  2. java bitmap jar_Java面试中常用的BitMap代码

    引言 阿里内推面试的时候被考了一道编程题:10亿个范围为1~2048的整数,将其去重并计算数字数目. 我看到这个题目就想起来了<编程珠玑>第一章讲的叫做BitMap的数据结构,但是我并没有 ...

  3. java bitmap 保存 jpg_Glide加载图片并保存到本地返回file,bitmap

    不废话,直接上代码 [java] view plain copy print ? importandroid.content.Context; importandroid.content.Intent ...

  4. java bitmap 点阵_Android Bitmap(点阵图像、绘制图像)

    Bitmap 称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的,这些点通过不同的排列和染色以构成图样. Bitmap 是 Android 系统中图像处理最重要的类之一,用它可以获取图像文 ...

  5. java bitmap 位图

    Java 有bitset集合,但是没有bitmap,bit*就是位图,代码如下<代码段> 有何用? 我们平时存储数据到集合,一般用hashmap,存储的基本单位就是字节,像Java基本类型 ...

  6. java bitmap图片_Bitmap图片的处理

    一.View转换为Bitmap 在Android中所有的控件都是View的直接子类或者间接子类,通过它们可以组成丰富的UI界面.在窗口显示的时候Android会把这些控件都加载到内存中,形成一个以Vi ...

  7. java bitmap base64_Android Bitmap到Base64字符串

    试试这个,首先将图像缩放到所需的宽度和高度,只需将原始位图,所需宽度和所需高度传递给以下方法,然后获得缩放位图: 例如:Bitmap scaledBitmap = getScaledBitmap(or ...

  8. java bitmap获取图片大小_android 通过uri获取bitmap图片并压缩

    很多人在调用图库选择图片时会在onActivityResult中用Media.getBitmap来获取返回的图片,如下: Uri mImageCaptureUri = data.getData(); ...

  9. java bitmap取出数据库_bitmap一般如何取出其所表示的数据(以java为例)

    利用bitmap进行排序,装入数据的时候只需要位运算设置1,那么取出数据的时候一般是怎么把标记为1的位转换成10进制的数?例如假设一个byte存储0-7的数据信息,那么如何将0000,1000转换成3 ...

最新文章

  1. JavaWeb--过滤器
  2. 三层代码讲解--第一课
  3. IN-12辉光数码管:俄罗斯进口的器件
  4. We Chall-Prime Factory-Writeup
  5. 初学Java-循环输入直到文件结束
  6. python 魔术方法
  7. Docker--------企业级最佳入门
  8. python数据挖掘orange
  9. Form窗体点击关闭按钮并未关闭进程的解决方法
  10. 噪声和振动分析软件,它代表了统计能量分析(SEA)领域的最高水平AutoSEA2 V2.8
  11. linux环境snmptrap告警命令中间服务器接收和转发配置
  12. kibana6.0版本汉化工具
  13. Ubuntu下制作deb包的方法详解
  14. 设置div高度为浏览器可视窗口的高度
  15. C语言重点——指针篇(一文让你完全搞懂指针)| 从内存理解指针 | 指针完全解析
  16. AI一周热闻:12306数据泄露,嫌疑人被捕;BERT提升文档检索性能至1.5-2倍
  17. 青龙面板之九章油条(更新)
  18. Zephyr_FileSystems
  19. 怎么将图片裁剪成想要的尺寸?建议收藏这些方法
  20. 开源的python有限元软件_想从Abaqus转用开源有限元软件有什么好的推荐吗?

热门文章

  1. LeetCode 375. Guess Number Higher or Lower II
  2. 【Problem solved】 error C2665: “loadimage”: 2 个重载中没有一个可以转换所有参数类型...
  3. Qt全局热键(windows篇)
  4. 关于页面加载的方法收集
  5. C#线程从陌生到熟悉(4)
  6. Android可视化界面开发工具DroidDraw
  7. java线程经典代码_Java线程代码实现
  8. python中文版-Python中文版
  9. python手机版怎么用-QPython,一个在手机上运行Python的神器
  10. python职能-最受欢迎的10家互联网公司 Python薪资揭秘!