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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。