當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。