我在Java中使用BitSet类来处理比特集。在比较两个BitSet时,我需要明确区分子集的概念和交叉的概念。
让我们看一个使用AND操作符来获取子集的示例:
BitSet bits1 = new BitSet();
BitSet bits2 = new BitSet();
bits1.set(0,2,true); //110
bits2.set(1); //010
//010 is a SUBSET of 110
bits1.and(bits2); //bits1 became the result of the and operator
if(bits1.equals(bits2))
{
System.out.println(bits2 + " is a subset of " + bits1);
}
//PRINT
BitSet bits4 = new BitSet();
bits4.set(0,2,true); //110
BitSet bits3 = new BitSet();
bits3.set(1,3,true); //011
bits4.and(bits3);
//011 is NOT a subset of 110
if(bits4.equals(bits3))
{
System.out.println(bits4 + " is a subset of " + bits3);
}
//NO PRINT子集非常清楚,因为我使用AND运算符来验证BitSet是另一个子集的子集。
与内置的交集运算符相同的示例:
BitSet bits1 = new BitSet();
BitSet bits2 = new BitSet();
bits1.set(0,2,true); //110
bits2.set(1); //010
//010 intersect 110, but is also a subset of 110
System.out.println("Intersection? " + bits2.intersects(bits1));
BitSet bits3 = new BitSet();
bits3.set(1,3,true); //011
//011 VS 110 intersection only
System.out.println("Intersection? " + bits3.intersects(bits1));我的问题是:操作符交集同时检测子集和交集。我的目标是只检测那些也是子集的交叉口,比如第二个例子中的bits1和bits2。所以这个操作符不适合我的情况,因为太笼统了。是否有检测此属性的方法?
发布于 2016-11-18 21:14:16
以bits1 bits2和bits2 1和(Bits2)的基数为例。如果与基数为非零,则集合相交.如果它也等于bits1基数,那么bits1是bits2的子集,反之亦然。
因此,使用基数,您可以根据需要检查子集关系(但它似乎比您在回答中已经提到的检查要快得多,并且可以组合起来)。
https://stackoverflow.com/questions/40685878
复制相似问题