ビットマップBitMapビッグデータフィルタ
26430 ワード
package oom;
/**
* index 1 , index 1, 0 ,
* 1。 5 ( 32) , int 32 (2 5 32)。
* &31 32, int 32 。
* int, , byte,long ,
* ( byte ) >>5 >>3,&31 &7
* BitMap
* : bitMap index 1
*/
public class BitMap {
private long length;
private static int[] bitsMap;
//
public BitMap(long length) {
this.length = length;
// ,
bitsMap = new int[(int) (length >> 5) + ((length & 31) > 0 ? 1 : 0)];
}
public int getBit(long index) {
int intData = bitsMap[(int) ((index - 1) >> 5)];
int offset = (int) ((index - 1) & 31);
return intData >> offset & 0x01;
}
public void setBit(long index) {
// index - 1 bitMap
int belowIndex = (int) ((index - 1) >> 5);
// ( )
int offset = (int) ((index - 1) & 31);
int inData = bitsMap[belowIndex];
bitsMap[belowIndex] = inData | (0x01 << offset);
}
public static void main(String[] args) {
BitMap bitMap = new BitMap(32);
bitMap.setBit(32);
System.out.println(bitMap.getBit(1));
System.out.println(bitMap.getBit(32));
}
}
package oom;
import java.util.Arrays;
/** (& )
* h & (length-1); h%length , &
JAVA , :
byte :8 bit (bit )
byte :1 (byte )
char :2
short :2
int :4
boolean :4
float :4
long :8
double :8
JVM ,boolean int , 4 ;boolean byte 。
* BitMap 0
*/
public class BitMapRepRemove {
//public static final int _1MB = 1024 * 1024;
//public static byte[] flags = new byte[ 512 * _1MB ];
public static byte[] flags;
public static void main(String[] args) {
int[] array = {255, 1024, 1024, 0, 65536, 0, 1024, 8888, 9999, 1111, 8888};
int length = 65536 + 1;
flags = new byte[(int) (length >> 3) + ((length & 7) > 0 ? 1 : 0)];
int index = 0;
for(int num : array) {
if( getFlags(num) != 1) {
//
array[index] = num;
index = index + 1;
//
setFlags(num);
}
}
array = Arrays.copyOf(array, index);
System.out.println(Arrays.toString(array));
System.out.println(array.length);
}
public static void setFlags(int num) {
int offset = num & (0x07);
flags[num >> 3] |= 0x01 << offset;
}
public static int getFlags(int num) {
int offset = num & (0x07);
return flags[num >> 3] >> offset & 0x01;
}
}
/*
* SoftReference , GC
*
*/
private Map<String, SoftReference<Bitmap>> imageMap
= new HashMap<String, SoftReference<Bitmap>>();
public Bitmap loadBitmap(final String imageUrl,final ImageCallBack imageCallBack) {
SoftReference<Bitmap> reference = imageMap.get(imageUrl);
if(reference != null) {
if(reference.get() != null) {
return reference.get();
}
}
final Handler handler = new Handler() {
public void handleMessage(final android.os.Message msg) {
//
Bitmap bitmap = (Bitmap)msg.obj;
imageMap.put(imageUrl, new SoftReference<Bitmap>(bitmap));
if(imageCallBack != null) {
imageCallBack.getBitmap(bitmap);
}
}
};
new Thread(){
public void run() {
Message message = handler.obtainMessage();
message.obj = downloadBitmap(imageUrl);
handler.sendMessage(message);
}
}.start();
return null ;
}
//
private Bitmap downloadBitmap (String imageUrl) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(new URL(imageUrl).openStream());
return bitmap ;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public interface ImageCallBack{
void getBitmap(Bitmap bitmap);
}