Java BitSet equals()方法及示例
Java BitSet类的equals()方法是用来检查两个比特集之间是否相等。它验证作为参数传递的一个集合的元素是否与这个集合的元素相等。如果比特集匹配,该方法返回真,否则返回假。
语法
Bit_Set1.equals(Bit_Set2)
参数: 该方法接受一个比特集类型的参数Bit_Set2,指的是要用这个比特集检查其平等性的集合。
返回值: 如果两个对象集的平等性都成立,该方法返回真,否则返回假。
下面的程序说明了Java中BitSet equals()方法的工作。
程序1 :
// 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
程序2
// 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