使用BitMap进行海量数据去重

问题:5TB的硬盘上放满了数据,请写一个算法将这些数据进行排重。如果这些数据是一些32bit大小的数据该如何解决?

参考文档链接:https://blog.csdn.net/zdxiq000/article/details/57626464

package com.demo;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author iamswf
 * @date 2021-03-03 14:55
 */
public class Main {
    /**
     * 单位Byte, 1K = 1024Byte, 1MB = 1024K
     */
    public static final int MB_SIZE = 1024 * 1024;

    /**
     * 1 Byte = 8 bit = 2^3 bit
     * 1KB = 1024Byte, 1MB = 1024KB
     *
     * 32位所需要的存储空间:2^32 bit = 2^29 Byte = 2^9 MB
     */
    public static byte[] byteFlags = new byte[512 * MB_SIZE];
    public static int[] intFlags = new int[512 * MB_SIZE / 4];


    public static void main(String[] args) {
        //待判重数据
        int[] array = {255, 1024, 0, 65536, 255, 1024};


        List<Integer> deduplicatedList = Lists.newArrayList();

        for (int i = 0; i < array.length; i++) {
            if (getFlag(array[i])) {
                break;
            } else {
                setFlag(array[i]);
                deduplicatedList.add(array[i]);
            }
        }

        System.out.println(deduplicatedList);

    }

    public static void setFlag(int num) {
        /**
         * 计算flag数组索引:即看位于第几个intFlag内
         */
        int intFlagIndex = 255 >> 5;
        // int intFlagIndex = (int)Math.floor(a32SizeNumber / 32);

        /**
         * 计算在intFlag上的偏移位置
         */
        int intFlagOffset = num & 31;
        // int intFlagOffset = a32SizeNumber % 32;

        // 设置flag
        intFlags[intFlagIndex] |= 0b1 << intFlagOffset;
    }

    public static boolean getFlag(int num) {
        return (intFlags[num >> 5] >> (num & 31) & 0b1) == 0b1;
    }
}
原文地址:https://www.cnblogs.com/iamswf/p/14540096.html