📌  相关文章
📜  Java中的 ConcurrentLinkedDeque equals() 方法及示例

📅  最后修改于: 2022-05-13 01:54:34.738000             🧑  作者: Mango

Java中的 ConcurrentLinkedDeque equals() 方法及示例

Java.util.ConcurrentLinkedDeque类的equals()方法用于比较指定对象与此 ConcurrentLinkedDeque 是否相等。当且仅当指定对象也是 ConcurrentLinkedDeque,两个 ConcurrentLinkedDeque 具有相同大小,并且两个 ConcurrentLinkedDeque 中所有对应的元素对相等时,才返回 true。 (如果 (e1==null ? e2==null : e1.equals(e2)) 两个元素 e1 和 e2 相等。)换句话说,如果两个 ConcurrentLinkedDeques 包含相同顺序的相同元素,则它们被定义为相等.

句法:

public boolean equals(Object o)

参数:此方法将对象 o作为参数,以便与此 ConcurrentLinkedDeque 进行比较是否相等。

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

下面的程序说明了 ConcurrentLinkedDeque.equals() 方法:

方案一:

// Java Program Demonstrate equals()
// method of ConcurrentLinkedDeque
  
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque CLD1
            = new ConcurrentLinkedDeque();
  
        // Add numbers to end of ConcurrentLinkedDeque
        CLD1.add(7855642);
        CLD1.add(35658786);
        CLD1.add(5278367);
        CLD1.add(74381793);
  
        System.out.println("Linked Blocking Deque 1: " + CLD1);
  
        // create another object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque CLD2
            = new ConcurrentLinkedDeque();
  
        // Add numbers to end of ConcurrentLinkedDeque
        CLD2.add("1");
        CLD2.add("2");
        CLD2.add("3");
        CLD2.add("4");
  
        System.out.println("Linked Blocking Deque 2: " + CLD2);
  
        // using equals() function
        System.out.println("Are both Linked Blocking Deque equal: "
                           + CLD1.equals(CLD2));
    }
}
输出:
Linked Blocking Deque 1: [7855642, 35658786, 5278367, 74381793]
Linked Blocking Deque 2: [1, 2, 3, 4]
Are both Linked Blocking Deque equal: false

方案二:

// Java Program Demonstrate equals()
// method of ConcurrentLinkedDeque
// when the list contains characters
  
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque CLD1
            = new ConcurrentLinkedDeque();
  
        // Add numbers to end of ConcurrentLinkedDeque
        CLD1.add("1");
        CLD1.add("2");
        CLD1.add("3");
        CLD1.add("4");
  
        System.out.println("Linked Blocking Deque 1: " + CLD1);
  
        // using equals() function
        System.out.println("Is CLD1 equal to CLD1: "
                           + CLD1.equals(CLD1));
    }
}
输出:
Linked Blocking Deque 1: [1, 2, 3, 4]
Is CLD1 equal to CLD1: true