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


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


java.util.HashSet类的equals()方法用于验证Object与HashSet的相等性并进行比较。仅当两个HashSet包含相同顺序的相同元素时,列表才返回true。

用法:

public boolean equals(Object o)

参数:此方法将对象o作为要与该集合相等性进行比较的参数。


返回值:如果指定对象等于此集合,则此方法返回true。

以下示例说明了equals()方法。

示例1:

// Java program to demonstrate equals() 
// method of HashSet 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
  
        // Creating object of HashSet<String> 
        HashSet<String> 
            arrset1 = new HashSet<String>(); 
  
        // Populating arrset1 
        arrset1.add("A"); 
        arrset1.add("B"); 
        arrset1.add("C"); 
        arrset1.add("D"); 
        arrset1.add("E"); 
  
        // print arrset1 
        System.out.println("First HashSet: "
                           + arrset1); 
  
        // Creating another object of HashSet<String> 
        HashSet<String> 
            arrset2 = new HashSet<String>(); 
  
        // Populating arrset2 
        arrset2.add("A"); 
        arrset2.add("B"); 
        arrset2.add("C"); 
        arrset2.add("D"); 
        arrset2.add("E"); 
  
        // print arrset2 
        System.out.println("Second HashSet: "
                           + arrset2); 
  
        // comparing first HashSet to another 
        // using equals() method 
        boolean value 
            = arrset1.equals(arrset2); 
  
        // print the value 
        System.out.println("Are both set equal: "
                           + value); 
    } 
}
输出:
First HashSet: [A, B, C, D, E]
Second HashSet: [A, B, C, D, E]
Are both set equal: true

示例2:

// Java program to demonstrate equals() 
// method of HashSet 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
    { 
  
        // Creating object of HashSet 
        HashSet<Integer> 
            arrset1 = new HashSet<Integer>(); 
  
        // Populating arrset1 
        arrset1.add(10); 
        arrset1.add(20); 
        arrset1.add(30); 
        arrset1.add(40); 
        arrset1.add(50); 
  
        // print arrset1 
        System.out.println("First HashSet: "
                           + arrset1); 
  
        // Creating another object of HashSet 
        HashSet<Integer> 
            arrset2 = new HashSet<Integer>(); 
  
        // Populating arrset2 
        arrset2.add(10); 
        arrset2.add(20); 
        arrset2.add(30); 
  
        // print arrset2 
        System.out.println("Second HashSet: "
                           + arrset2); 
  
        // comparing first HashSet to another 
        // using equals() method 
        boolean value = arrset1.equals(arrset2); 
  
        // print the value 
        System.out.println("Are both set equal: "
                           + value); 
    } 
}
输出:
First HashSet: [50, 20, 40, 10, 30]
Second HashSet: [20, 10, 30]
Are both set equal: false


相关用法


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