本篇主要实现两种类型(String & Long)转化为bitMap逻辑。
第一种:String 转化为bitMap
大致逻辑是先将String 对应到一个 Long类型的数字longMax,内部从0开始,最大值是Long.MAX_VALUE=2^63.再将Long对应到bit链表中,longMax与bit的对应关系保存在bitMap当中。
其中有3个全局变量:
/* recond max long */
private Long longMax = 0l;
/* recond longMax to bit*/
private ArrayList<Long> bit = new ArrayList<>();
/* recond String to bit value */
private HashMap<String, Long> bitMap = new HashMap<>();
重点有以下几个函数:
addBitMap():添加String到bitMap,可以传输单个String,也可以传输String数组。
delBitMap():删除bitMap中的String,可以传输单个String,也可以传输String数组。如果删除的String不存在,不会报错。
getStrBitMap():获取String对应的bitMap,可以传输单个String,也可以传输String数组。
这里的重点算法是Long数据如何对应到bit链表中,基本流程如下:
第一步:l >> 6 求出数值在数组的那个下标中,这里注意的是Long类型的bit位数(64=2^6位)。
第二步:1l << ((l - 1) & 0x3f) 求出数值放在指定下标的那个位上(这里当index与bit.size()相等时除外,见下面代码)。
这里就相当于0~63放到bit[0],64~127放到bit[1]......
/* add Long to bit type */
private void addLongToBit(Long l) {
/* get index while need to insert */
int index = (int) (l >> 6);
if(index == bit.size()) {
bit.add(1l);
}else {
Long value = bit.get(index);
bit.set(index, value | (1l << ((l - 1) & 0x3f)));
}
}
以下是这个功能的整体代码:
package com.bitmap;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @func String to bitMap type
* @author YUFUCHENG
* @data 2019-06-15
*
*
*/
public class StrToBitMap {
/* recond max long */
private Long longMax = 0l;
/* recond longMax to bit*/
private ArrayList<Long> bit = new ArrayList<>();
/* recond String to bit value */
private HashMap<String, Long> bitMap = new HashMap<>();
public ArrayList<Long> getBit() {
return bit;
}
public Long getLongMax() {
return longMax;
}
public StrToBitMap() {}
public StrToBitMap(String str) {
this.addBitMap(str);
}
public StrToBitMap(String[] strs) {
this.addBitMap(strs);
}
/* add Long to bit type */
private void addLongToBit(Long l) {
/* get index while need to insert */
int index = (int) (l >> 6);
if(index == bit.size()) {
bit.add(1l);
}else {
Long value = bit.get(index);
bit.set(index, value | (1l << ((l - 1) & 0x3f)));
}
}
/* delete Long to bit type*/
private void delLongToBit(Long l) {
/* get index while need to insert */
int index = (int) (l >> 6);
if(index >= bit.size()) {
return;
}else {
Long value = bit.get(index);
bit.set(index, value & (~(1l << ((l - 1) & 0x3f))));
}
}
/* add str to bitMap and update longMax */
public boolean addBitMap(String str) {
String[] strs = {str};
return this.addBitMap(strs);
}
/* add strs to bitMap and update bitMax */
public boolean addBitMap(String[] strs) {
if(strs.length == 0) {
return false;
}
for (String str : strs) {
if(bitMap.isEmpty()) {
bitMap.put(str, ++longMax);
addLongToBit(longMax);
continue;
}
if(!bitMap.containsKey(str)) {
bitMap.put(str, ++longMax);
addLongToBit(longMax);
}else {
System.out.println("String: " + str + " exsist in bitMap! this value: " + getBitMap(str));
}
}
return true;
}
/* delete str to bitMap */
public boolean delBitMap(String str) {
String[] strs = {str};
return this.delBitMap(strs);
}
/* delete strs to bitMap */
public boolean delBitMap(String[] strs) {
if(strs.length == 0) {
return false;
}
for (String str : strs) {
if(bitMap.isEmpty()) {
return false;
}else if(bitMap.containsKey(str)) {
Long l = bitMap.remove(str);
delLongToBit(l);
continue;
}else {
continue;
}
}
return true;
}
/* get long in bitMap */
private Long getBitMap(String str) {
return bitMap.get(str);
}
public HashMap<String, Long> getStrBitMap(String str){
String[] strs = new String[] {str};
return getStrBitMap(strs);
}
/* get strs in bitMap, str is not exsist in bitMap if vlaue is null */
public HashMap<String, Long> getStrBitMap(String[] strs) {
HashMap<String, Long> strsBitMap = new HashMap<String, Long>();
for(String str : strs) {
if(bitMap.containsKey(str)) {
strsBitMap.put(str, bitMap.get(str));
}else {
strsBitMap.put(str, null);
System.out.println("String: " + str + " is not exsist in bitMap!");
}
}
return strsBitMap;
}
/* get all longMap */
public HashMap<String, Long> getAllBitMap() {
return bitMap;
}
}
第二种:Long 转化为bitMap
这种转换相比String的转化较简单一些,根据用户输入的Long直接对应到bitMap中,用bit保存用户的输入,bitMap是对应的Long数据值位置。算法和String转化为bitMap一样。
其中有2个全局变量:
/* recond longMax to bit*/
private ArrayList<Long> bit = new ArrayList<>();
/* recond String to bit value */
private ArrayList<Long> bitMap = new ArrayList<>();
这里逻辑和String转化为bitMap一样,比第一种少了String对应到Long的步骤。下面就直接贴代码了:
package com.bitmap;
import java.util.ArrayList;
public class LongToBitMap {
/* recond longMax to bit*/
private ArrayList<Long> bit = new ArrayList<>();
/* recond String to bit value */
private ArrayList<Long> bitMap = new ArrayList<>();
/* get bitMap size*/
public int getLongSize() {
return bitMap.size();
}
public LongToBitMap() {}
public LongToBitMap(Long l) {
this.addBitMap(l);
}
public LongToBitMap(Long[] ls) {
this.addBitMap(ls);
}
/* add Long to bit type */
private void addLongToBit(Long l) {
/* get index while need to insert */
int index = (int) (l >> 6);
if(index == bit.size()) {
bit.add(1l);
}else {
Long value = bit.get(index);
bit.set(index, value | (1l << ((l - 1) & 0x3f)));
}
}
/* delete Long to bit type*/
private void delLongToBit(Long l) {
/* get index while need to insert */
int index = (int) (l >> 6);
if(index >= bit.size()) {
return;
}else {
Long value = bit.get(index);
if(l == 0l) {
bit.set(index, value & (Long.MAX_VALUE -1));
}else {
bit.set(index, value & (~(1l << ((l - 1) & 0x3f))));
}
}
}
/* add str to bitMap and update longMax */
public boolean addBitMap(Long l) {
Long[] ls = {l};
return this.addBitMap(ls);
}
/* add strs to bitMap and update bitMax */
public boolean addBitMap(Long[] ls) {
if(ls.length == 0) {
return false;
}
for (Long l : ls) {
if(bitMap.isEmpty()) {
bitMap.add(l);
continue;
}
if(!bitMap.contains(l)) {
bitMap.add(l);
addLongToBit(l);
}else {
System.out.println("Long: " + l + " exsist in bitMap!");
}
}
return true;
}
/* delete str to bitMap */
public boolean delBitMap(Long l) {
Long[] ls = {l};
return this.delBitMap(ls);
}
/* delete strs to bitMap */
public boolean delBitMap(Long[] ls) {
if(ls.length == 0) {
return false;
}
for (Long l : ls) {
if(bitMap.isEmpty()) {
return false;
}else if(bitMap.contains(l)) {
bitMap.remove(l);
delLongToBit(l);
continue;
}else {
continue;
}
}
return true;
}
public ArrayList<Long> getBit() {
return bit;
}
public ArrayList<Long> getBitMap() {
return bitMap;
}
}
以上就是这篇文章的所有内容了。
待补充内容:
其中bit这个ArrayList<Long>类型的全局变量还没有用到,这个变量的功能可以用来计算String或Long的数据量,总的来说有以下几个功能:
1.去重统计
2.排序(顺序、倒叙)
3.留存统计
4.新增统计