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


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


java.util.TreeSet類的equals()方法用於將指定對象與此集進行比較,以確保相等。當且僅當指定對象也是一個集合,並且兩個集合具有相同的大小,並且兩個集合中所有對應的元素對相等時,才返回true。 (如果(e1 == null?e2 == null:e1.equals(e2)),則兩個元素e1和e2相等。)換句話說,如果兩個集合包含相同順序的相同元素,則它們被定義為相等。 。

用法:

public boolean equals(Object o)

參數:此方法將對象o作為要與該集合相等性進行比較的參數。


返回值:如果指定對象等於此集合,則此方法返回true。

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

示例1:

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

示例2:

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


相關用法


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