当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BitSet equals()用法及代码示例


Java BitSet类的equals()方法用于检查两个位集之间的相等性。它验证作为参数传递的一组元素是否等于该元素。如果这些位集匹配,则该方法返回true,否则返回false。

用法:

Bit_Set1.equals(Bit_Set2)

参数:该方法接受位集类型的一个参数Bit_Set2表示要使用该位集检查其相等性的集。


返回值:如果两个对象集的相等性成立,则该方法返回true,否则返回false。

下面的程序演示了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


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 BitSet equals() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。