Java中的 BitSet equals() 方法及示例
Java BitSet 类的 equals() 方法用于检查两个位集之间的相等性。它验证作为参数传递的一个集合的元素是否等于该集合的元素。如果位集匹配,则该方法返回 true,否则返回 false。
句法:
Bit_Set1.equals(Bit_Set2)
参数:该方法接受一个位集类型的参数Bit_Set2 ,并引用要与该位集检查其相等性的集。
返回值:如果相等性对两个对象集都成立,则该方法返回 true,否则返回 false。
下面的程序说明了Java中 BitSet equals() 方法的工作原理。
方案一:
// Java code to illustrate equals()
import java.util.*;
public class BitSet_Demo {
public static void main(String args[])
{
// Creating an empty BitSet
BitSet bit_set1 = new BitSet();
BitSet bit_set2 = new BitSet();
// Use set() method to add elements into the Set
bit_set1.set(40);
bit_set1.set(25);
bit_set1.set(80);
bit_set1.set(95);
bit_set1.set(5);
// Use set() method to add elements into the Set
bit_set2.set(25);
bit_set2.set(40);
bit_set2.set(5);
bit_set2.set(95);
bit_set2.set(80);
// Displaying the BitSets
System.out.println("First BitSet: " + bit_set1);
System.out.println("Second BitSet: " + bit_set2);
// Checking for equality
System.out.println("Are the sets equal? "
+ bit_set1.equals(bit_set2));
}
}
输出:
First BitSet: {5, 25, 40, 80, 95}
Second BitSet: {5, 25, 40, 80, 95}
Are the sets equal? true
方案二:
// Java code to illustrate equals()
import java.util.*;
public class BitSet_Demo {
public static void main(String args[])
{
// Creating an empty BitSet
BitSet bit_set1 = new BitSet();
BitSet bit_set2 = new BitSet();
// Use set() method to add elements into the Set
bit_set1.set(40);
bit_set1.set(25);
bit_set1.set(80);
bit_set1.set(95);
bit_set1.set(5);
// Use set() method to add elements into the Set
bit_set2.set(10);
bit_set2.set(20);
bit_set2.set(30);
bit_set2.set(40);
bit_set2.set(50);
// Displaying the BitSets
System.out.println("First BitSet: " + bit_set1);
System.out.println("Second BitSet: " + bit_set2);
// Checking for equality
System.out.println("Are the sets equal? "
+ bit_set1.equals(bit_set2));
}
}
输出:
First BitSet: {5, 25, 40, 80, 95}
Second BitSet: {10, 20, 30, 40, 50}
Are the sets equal? false