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)); }

得到有序結果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM