大数据位图法(无重复排序,重复排序,去重复排序,数据压缩)之Java实现

1,位图法介绍

    位图的基本概念是用一个位(bit)来标记某个数据的存放状态,由于采用了位为单位来存放数据,所以节省了大量的空间。举个具体的例子,在Java中一般一个int数字要占用32位,如果能用一位就表示这个数,就可以缩减大量的存储空间。一般把这种方法称为位图法,即Bitmap。

    位图法比较适合于判断是否存在这样的问题,元素的状态比较少,元素的个数比较多的情况之下。那么具体咋么做呢,这样,非常简单明了就是,2.5亿个整数里面,我维护一个长度等于最大整数值得字符串,每个整数是否存在我就在该整数对应的位置置为1,比如,有{2, 4, 5, 6, 67, 5}这么几个整数,我维护一个 00…0000 67位的字符串。但是,如果你不知道整数的最大值,你至少需要一个长度2^32的字符串,因为整数的最大值就是2^32,(int占4个字节,因此是32位),那这就最少是512M内存,从char的长度算内存会算吧,直接、最大整数/8*2^20 就是M的单位。那这么说来就可以理解位图法了。

2,BitSet

    正因为位图运算在空间方面的优越性,很多语言都有直接对它的支持。如在C++的STL库中就有一个bitset容器。而在Java中,在java.util包下也有一个BitSet类用来实现位图运算。此类实现了一个按需增长的位向量。BitSet的每一位都由一个boolean值来表示。用非负的整数将BitSet的位编入索引,可以对每个编入索引的位进行测试、设置或者清除。通过逻辑与、逻辑或和逻辑异或操作,可以使用一个BitSet修改另一个BitSet的内容。

    需要注意的是BitSet底层实现是通过一个long数组来保存数据的,也就是说它增长的最小单位是一个long所占的逻辑位,即64位。但如果不是对存储区空间有极致的要求,而且对自己的基本功非常有信心,不建议自己去实现一个跟BitSet类似的类来实现相关的功能。因为jdk中的类都是极精简并做过合理优化的,BitSet类比较长。

3,无重复排序

java JDK里面容器类的排序算法使用的主要是插入排序和归并排序,可能不同版本的实现有所不同,关键代码如下:

 1 /**
 2      * Performs a sort on the section of the array between the given indices
 3      * using a mergesort with exponential search algorithm (in which the merge
 4      * is performed by exponential search). n*log(n) performance is guaranteed
 5      * and in the average case it will be faster then any mergesort in which the
 6      * merge is performed by linear search.
 7      * 
 8      * @param in -
 9      *            the array for sorting.
10      * @param out -
11      *            the result, sorted array.
12      * @param start
13      *            the start index
14      * @param end
15      *            the end index + 1
16      */
17     @SuppressWarnings("unchecked")
18     private static void mergeSort(Object[] in, Object[] out, int start,
19             int end) {
20         int len = end - start;
21         // use insertion sort for small arrays
22         if (len <= SIMPLE_LENGTH) {
23             for (int i = start + 1; i < end; i++) {
24                 Comparable<Object> current = (Comparable<Object>) out[i];
25                 Object prev = out[i - 1];
26                 if (current.compareTo(prev) < 0) {
27                     int j = i;
28                     do {
29                         out[j--] = prev;
30                     } while (j > start
31                             && current.compareTo(prev = out[j - 1]) < 0);
32                     out[j] = current;
33                 }
34             }
35             return;
36         }
37         int med = (end + start) >>> 1;
38         mergeSort(out, in, start, med);
39         mergeSort(out, in, med, end);
40 
41         // merging
42 
43         // if arrays are already sorted - no merge
44         if (((Comparable<Object>) in[med - 1]).compareTo(in[med]) <= 0) {
45             System.arraycopy(in, start, out, start, len);
46             return;
47         }
48         int r = med, i = start;
49 
50         // use merging with exponential search
51         do {
52             Comparable<Object> fromVal = (Comparable<Object>) in[start];
53             Comparable<Object> rVal = (Comparable<Object>) in[r];
54             if (fromVal.compareTo(rVal) <= 0) {
55                 int l_1 = find(in, rVal, -1, start + 1, med - 1);
56                 int toCopy = l_1 - start + 1;
57                 System.arraycopy(in, start, out, i, toCopy);
58                 i += toCopy;
59                 out[i++] = rVal;
60                 r++;
61                 start = l_1 + 1;
62             } else {
63                 int r_1 = find(in, fromVal, 0, r + 1, end - 1);
64                 int toCopy = r_1 - r + 1;
65                 System.arraycopy(in, r, out, i, toCopy);
66                 i += toCopy;
67                 out[i++] = fromVal;
68                 start++;
69                 r = r_1 + 1;
70             }
71         } while ((end - r) > 0 && (med - start) > 0);
72 
73         // copy rest of array
74         if ((end - r) <= 0) {
75             System.arraycopy(in, start, out, i, med - start);
76         } else {
77             System.arraycopy(in, r, out, i, end - r);
78         }
79     }

 

    下面我们说下位图法排序的思路:其实思路开篇已经交代,为了让大家更容易理解,我将通过举例的方式进一步阐明,假设我们有一个不重复的整型序列{n1, n2, ... ,nn},假设最大值为nx,则我们可以维护一个长度为nx的位串,第一遍遍历整个序列,将出现的数字在位串中对应的位置置为1;第二遍遍历位图,依次输出值为1的位对应的数字,这些1所在的位串中的位置的索引代表序列数据,1出现的先后位置则代表序列的大写。

下面按上面的原理用Java实现:

 

 1 package acm;
 2 
 3 import java.util.*;
 4 
 5 public class javaUniqueSort {
 6     public static int[] temp = new int[100001];
 7     public static List<Integer> tempList = new ArrayList<Integer>();
 8     public static int count ;
 9     public static long start ;
10     public static long end ;
11     
12     public static List<Integer> uniqueSort(final List<Integer> uniqueList) {
13         javaUniqueSort.tempList.clear();
14         for (int i = 0; i < javaUniqueSort.temp.length; i++) {
15             javaUniqueSort.temp[i] = 0;
16         }
17         for (int i = 0; i < uniqueList.size(); i++) {
18             javaUniqueSort.temp[uniqueList.get(i)] = 1;
19         }
20         for (int i = 0; i < javaUniqueSort.temp.length; i++) {
21             if (javaUniqueSort.temp[i] == 1) {
22                 javaUniqueSort.tempList.add(i);
23             }
24         }
25 
26         return javaUniqueSort.tempList;
27     }
28 
29 
30     public static void getStartTime() {
31         javaUniqueSort.start = System.nanoTime();
32     }
33 
34     public static void getEndTime(final String s) {
35         javaUniqueSort.end = System.nanoTime();
36         System.out.println(s + ": " + (javaUniqueSort.end - javaUniqueSort.start) + "ns");
37     }
38     
39     
40     
41     public static void main(final String[] args) {
42         
43         List<Integer> firstNum = new ArrayList<Integer>();
44         List<Integer> secondNum = new ArrayList<Integer>();
45 
46         for (int i = 1; i <= 100000; i++) {
47             firstNum.add(i);
48             secondNum.add(i);
49         }
50 
51         Collections.shuffle(firstNum);
52         Collections.shuffle(secondNum);
53         
54     
55         getStartTime();
56         Collections.sort(firstNum);
57         getEndTime("java sort run time  ");
58 
59         getStartTime();
60         secondNum = uniqueSort(secondNum);
61         getEndTime("uniqueSort run time ");
62         
63 
64     }
65 }

 

执行结果

4,有重复排序

    有重复的整数序列排序,分为两种情况,保留重复的整数排序,和去除重复整数排序。

4.1 保留重复的整数排序

思路:上面讲述了无重复的整数序列排序,其实序列中的整数在位串中只用两个状态,要么在序列中出现(1),要么不出现(0),而对于有重复的整数序列,我们仍然可以用序列中整数出现的次数来表示数据状态,只是现在这个状态的数目是不确定的。实现方式也上面类似。

 

 1 package acm;
 2 
 3 import java.util.*;
 4 
 5 public class javaDuplicateSort {
 6     public static List<Integer> tempList = new ArrayList<Integer>();
 7     public static int count;
 8     public static long start ;
 9     public static long end ;
10     
11     public static void main(final String[] args) {
12         Random random = new Random();
13         List<Integer> firstNum = new ArrayList<Integer>();
14         List<Integer> secondNum = new ArrayList<Integer>();
15 
16         for (int i = 1; i <= 100000; i++) {
17             firstNum.add(i);
18             secondNum.add(i);
19             firstNum.add(random.nextInt(i + 1));
20             secondNum.add(random.nextInt(i + 1));
21         }
22         Collections.shuffle(firstNum);
23         Collections.shuffle(secondNum);
24 
25         getStartTime();
26         Collections.sort(firstNum);
27         getEndTime("java sort run time  ");
28 
29         getStartTime();
30         secondNum = uniqueSort(secondNum);
31         getEndTime("uniqueSort run time ");
32 
33     }
34 
35     public static List<Integer> uniqueSort(final List<Integer> uniqueList) {
36         javaDuplicateSort.tempList.clear();
37         int[] temp = new int[200002];
38         for (int i = 0; i < temp.length; i++) {
39             temp[i] = 0;
40         }
41         for (int i = 0; i < uniqueList.size(); i++) {
42             temp[uniqueList.get(i)]++;
43         }
44         for (int i = 0; i < temp.length; i++) {
45             for (int j = temp[i]; j > 0; j--) {
46                 javaDuplicateSort.tempList.add(i);
47             }
48         }
49 
50         return javaDuplicateSort.tempList;
51     }
52 
53     public static void getStartTime() {
54         javaDuplicateSort.start = System.nanoTime();
55     }
56 
57     public static void getEndTime(final String s) {
58         javaDuplicateSort.end = System.nanoTime();
59         System.out.println(s + ": " + (javaDuplicateSort.end - javaDuplicateSort.start) + "ns");
60     }
61 }

 

执行结果:

4.2 去除重复整数排序

 思路:去重的意思就是整数序列中多次出现的整数只保留一次,这也很好处理,可以对上面的方法再往前推一步,对位串中大于1的数全部置1,这样就把重复的数据给去除了(或者在排序的时候增设一个条件状态数大于1的,按1来处理,这样也能得到想要的结果),方法很多,看个人的喜好,这里我就不去实现了。

 

5,数据压缩

    假设有这样一份数据,记录了全国1990-1999年出生的人的姓名和出生年月的键值对。假设正好有一千万人,那就要存储一千万个姓名和年份。如何运用Bitmap的思想来压缩数据呢。下面提供几种思路。从人的角度来看,由于一共就只有10个年份,可以用4个bit将它们区分开。如0000表示1990年,1001表示1999年。那一个人的出生年份就可以用4个bit位来表示,进而一千万个年份就可以压缩为一千万个4位的bit组;从另一个角度来看这个问题,我们有10个年份,每个人要么是要么不是在这个年份出生。每个人对于年份来说就可以抽象为一个bit位,所以我们可以把一千万的年龄压缩为10个一千万位的bit组。这样压缩的力度不如按人的角度压缩的大,但从年份出发的问题会有一定的优势,如有哪些人是1990年出生的,只需遍历1990年对应的那个bit组就可以了。可以看出来不管从哪个角度,bitmap的压缩都是建立在数据中存在大量的冗余数据的基础上的,如年份。而在上面的问题中,年份的分布是散乱的,那假如我们事先把数据进行了排序,把相同的出生年份的人排在一起,那数据就可以进一步压缩。这样一来就只要记录每个年份的人数,就可以根据下标来判断每个人的出生年份。

 

总结

     位图法可以用于海量数据排序,海量数据去重,海量数据压缩,针对于稠密的数据集可以很好体现出位图法的优势(内存消耗少,速度较快),但对于稀疏数据集,应用位图法反而会适得其反,比如我们有一个长度为10的序列,最大值为20亿,则构造位串的内存消耗将相当大250M,而实际却只需要40个字节,此外位图法还存在可读性差等缺点。

 

参考文献:

https://jinfagang.gitlab.io/2017/09/01/%E4%B8%87%E5%8F%98%E4%B8%8D%E7%A6%BB%E5%85%B6%E5%AE%97%E4%B9%8B%E6%B5%B7%E9%87%8F%E6%95%B0%E6%8D%AE%E4%B8%8B%E7%9A%84%E7%AE%97%E6%B3%95%E9%97%AE%E9%A2%98%E5%A4%84%E7%90%86%E6%80%9D%E8%B7%AF/

http://blog.csdn.net/u013291394/article/details/50211181

http://blog.csdn.net/y999666/article/details/51220833

http://blog.csdn.net/korey_sparks/article/details/52512870

 

 

 

 

 

转载于:https://www.cnblogs.com/rrttp/p/7668773.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值