• java使用bitmap求两个数组的交集


    一般来说int代表一个数字,但是如果利用每一个位 ,则可以表示32个数字 ,在数据量极大的情况下可以显著的减轻内存的负担。我们就以int为例构造一个bitmap,并使用其来解决一个简单的问题:求两个数组的交集

    先实现一个bitmap

    /**
     * @Description:
     * @author: zhoum
     * @Date: 2020-01-23
     * @Time: 10:49
     */
    public class BitMap {
    
        private int[] sign = {0x00000001,0x00000002,0x00000004,0x00000008,0x00000010,0x00000020,0x00000040,0x00000080,0x00000100,0x00000200,0x00000400,0x00000800,0x00001000,0x00002000,0x00004000,0x00008000,
                0x00010000,0x00020000,0x00040000,0x00080000,0x00100000,0x00200000,0x00400000,0x00800000,0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,0x40000000,0x80000000};
    
        private int[] arr ;
    
        private int capacity;
    
        public BitMap(int capacity) {
            validate(capacity);
            this.capacity = capacity;
            this.arr = new int[(capacity>>5)+1];
        }
    
        public void put(int k){
            if ( k > capacity ){
                throw new RuntimeException("k is greater than capacity");
            }
            validate(k);
            int index = k >> 5 ;//当前数字应该存放的bucket索引
            arr[index] = arr[index]|sign[k & 31];
    
        }
    
    
        private void validate(int k){
            if ( k <= 0 ){
                throw new IllegalArgumentException(" capacity must be greater than zero");
            }
        }
    
    
        public int[] getMixed(BitMap bitMap){
            int length = Math.min(bitMap.arr.length,this.arr.length);
            int[] other = new int[length],me = new int[length];
            System.arraycopy(bitMap.arr,0,other,0,length);
            System.arraycopy(this.arr,0,me,0,length);
            //借用集合的无固定大小来构建最后数组
            List<Integer> result = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                int k= other[i] & me[i];
                for (int j = 1; j <= 32; j++) {
                    if ( ((k>>j)&1) == 1 ){
                        result.add((i<<5)+j);
                    }
                }
            }
            if ( result.size() == 0 ){
                return null;
            }else {
               int[] rs = new int[result.size()];
                for (int i = 0; i < result.size(); i++) {
                    rs[i] = result.get(i);
                }
                return rs;
            }
    
        }
    }

    写一个main方法试验下

        public static void main(String[] args) {
    BitMap bitMap = new BitMap(1000){{ put(248); put(5); put(9); put(12); put(6); put(13); put(963); }}; BitMap bitMap1 = new BitMap(1000){{ put(248); put(15); put(13); put(963); put(5); put(6); put(9); }}; int[] mixed = bitMap.getMixed(bitMap1); System.out.println(Arrays.toString(mixed)); }

    得到有序结果

  • 相关阅读:
    python基础教程(第二版)---1.11字符串
    python基础教程(第二版)---1.91(我只记录了cmath模块相关)
    python基础教程(第二版)---1.7获取用户输入(讲的是input函数)
    python基础教程(第二版)---1.6语句
    openGL 进行图像处理
    uchar4
    709 与 601 公式上的区别
    J420P 黑白红青蓝黄对应的YUV 值
    混音的方法
    CPU周期, 时钟周期, 机器周期, 指令周期 总线周期
  • 原文地址:https://www.cnblogs.com/hetutu-5238/p/12231643.html
Copyright © 2020-2023  润新知