/**
* 获取复合值查询的所有可能值
*
* @param value 复合值
* @param list 多个 2^n 值
* @return 所有满足条件的值
*/
public static List<Integer> group(Integer value,List<Integer> list) {
Set<Integer> valueSet =list.stream().filter(e -> (e & value) != 0).collect(Collectors.toSet());
List<Integer> result = new ArrayList<>();
for (int powI = 1,powBySize =(int) Math.pow(2, list.size()); powI < powBySize; powI++) {
//获取powI的二进制
List<String> binaryList = Arrays.asList(Integer.toBinaryString(powI).split(""));
//反转二进制
Collections.reverse(binaryList);
//获取组合方式
Set<Integer> groupNumSet = new HashSet<>();
for (int binaryJ = 0,size =binaryList.size(); binaryJ < size; binaryJ++) {
if ("1".equals(binaryList.get(binaryJ))) {
groupNumSet.add(Integer.parseInt(binaryList.get(binaryJ)) * list.get(binaryJ));
}
}
//组合后的值 是否包含 查询的值
if (groupNumSet.containsAll(valueSet)) {
result.add(groupNumSet.stream().reduce(0, (e1, e2) -> e1 | e2));
}
}
return result;
}
网友评论