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


Java AbstractSet equals()用法及代碼示例


Java AbstractSet中的AbstractSet.equals()方法用於檢查兩個集合之間的相等性。它驗證作為參數傳遞的一組元素是否等於該元素。

用法:

AbstractSet1.equals(AbstractSet2)

參數:該方法接受一個抽象集類型的參數AbstractSet2,表示要使用此抽象集檢查其相等性的集合。


返回值:如果兩個對象集都相等,則該方法返回true,否則返回false。

下麵的程序說明AbstractSet.equals()方法的用法:

示例1:

// Java code to illustrate the equals() method 
import java.util.*; 
  
public class Abstract_Set_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty AbstractSet 
        AbstractSet<String> 
            abstract_set1 = new HashSet<String>(); 
        AbstractSet<String> 
            abstract_set2 = new HashSet<String>(); 
  
        // Adding elements to set 
        abstract_set1.add("Geeks"); 
        abstract_set1.add("4"); 
        abstract_set1.add("Geeks"); 
        abstract_set1.add("Welcomes"); 
        abstract_set1.add("You"); 
  
        // Adding elements to set 
        abstract_set2.add("Geeks"); 
        abstract_set2.add("4"); 
        abstract_set2.add("Geeks"); 
        abstract_set2.add("Welcomes"); 
        abstract_set2.add("You"); 
  
        // Displaying the first HashSet 
        System.out.println("First Set: "
                           + abstract_set1); 
  
        // Displaying the second HashSet 
        System.out.println("Second Set: "
                           + abstract_set2); 
  
        // Displaying the equality 
        System.out.println("Equality: "
                           + abstract_set1 
                                 .equals(abstract_set2)); 
    } 
}
輸出:
First Set: [4, Geeks, You, Welcomes]
Second Set: [4, Geeks, You, Welcomes]
Equality: true

示例2:

// Java code to illustrate the equals() method 
import java.util.*; 
  
public class Abstract_Set_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty AbstractSet 
        AbstractSet<String> 
            abstract_set1 = new HashSet<String>(); 
        AbstractSet<String> 
            abstract_set2 = new HashSet<String>(); 
  
        // Adding elements to set 
        abstract_set1.add("Geeks"); 
        abstract_set1.add("4"); 
        abstract_set1.add("Geeks"); 
        abstract_set1.add("Welcomes"); 
        abstract_set1.add("You"); 
  
        // Adding elements to set 
        abstract_set2.add("Geeks"); 
        abstract_set2.add("4"); 
        abstract_set2.add("Geeks"); 
        abstract_set2.add("Welcomes"); 
        abstract_set2.add("U"); 
  
        // Displaying the first HashSet 
        System.out.println("First Set: "
                           + abstract_set1); 
  
        // Displaying the second HashSet 
        System.out.println("Second Set: "
                           + abstract_set2); 
  
        // Displaying the equality 
        System.out.println("Equality: "
                           + abstract_set1 
                                 .equals(abstract_set2)); 
    } 
}
輸出:
First Set: [4, Geeks, You, Welcomes]
Second Set: [4, U, Geeks, Welcomes]
Equality: false


相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 AbstractSet equals() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。