我想将很多应用程序功能存储到数据库中。我想为此使用一个表格行,所以我想使用位掩码来存储启用的功能。我试过这个:
HashMap<String, Integer> map = new HashMap<String, Integer>();
int database_value = 0;
public int setFeatureBitmask(int value) {
int bitmask = 0;
bitmask |= value;
return bitmask;
}
public static Set<String> processFeature(Integer bitmask) {
HashMap<String, Integer> newMap = new HashMap<String, Integer>();
return newMap.entrySet().stream().filter(e -> (e.getValue() & bitmask) == e.getValue()).map(e -> e.getKey())
.collect(Collectors.toSet());
}
@BeforeAll
public void setupData() {
System.out.println("Populating settings map");
map.put("Type1", 1);
map.put("Type2", 2);
map.put("Type3", 4);
map.put("Type4", 8);
map.put("Type5", 16);
map.put("Type6", 32);
map.put("Type7", 64);
map.put("Type8", 128);
map.put("Type9", 256);
map.put("Type10", 512);
map.put("Type11", 1024);
map.put("Type12", 2048);
map.put("Type13", 4096);
map.put("Type14", 8192);
map.put("Type15", 16384);
map.put("Type16", 32768);
map.put("Type17", 65536);
map.put("Type18", 131072);
map.put("Type19", 262144);
}
@Test
public void writeData() {
System.out.println("Converting Map using bitmasking");
for(int i=0; i<map.size(); i++) {
int number_value = map.get(i);
int result = setFeatureBitmask(number_value);
database_value = database_value + result;
}
}
@AfterAll
public void databaseInsert() {
System.out.println("Final resut to insert into Database " + database_value);
System.out.println("Converting back values from database");
// read here values from database_value variable and convert them into hash
}
第一个问题是例如如何将转换后的数字存储为一个大表值15990793?我应该将所有转换后的数字组合成一个大数字吗?而且在我转换它们之后如何再次翻译它们以便我可以使用它们?
有没有工作的例子?我发现了许多位掩码示例,但没有像我的案例那样完整。完全不清楚如何获得工作结果你能建议吗?
相关分类