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


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