📜  Java中的 LinkedBlockingDeque equals() 方法及示例

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

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

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

句法:

public boolean equals(Object o)

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

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

下面的示例说明了 LinkedBlockingDeque.equals() 方法:

方案一:

// Java Program Demonstrate equals()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD1
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD1.add(7855642);
        LBD1.add(35658786);
        LBD1.add(5278367);
        LBD1.add(74381793);
  
        System.out.println("Linked Blocking Deque 1: " + LBD1);
  
        // create another object of LinkedBlockingDeque
        LinkedBlockingDeque LBD2
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD2.add("1");
        LBD2.add("2");
        LBD2.add("3");
        LBD2.add("4");
  
        System.out.println("Linked Blocking Deque 2: " + LBD2);
  
        // using equals() function
        System.out.println("Are both Linked Blocking Deque equal: "
                           + LBD1.equals(LBD2));
    }
}
输出:
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 LinkedBlockingDeque
// when the list contains characters
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD1
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD1.add("1");
        LBD1.add("2");
        LBD1.add("3");
        LBD1.add("4");
  
        System.out.println("Linked Blocking Deque 1: " + LBD1);
  
        // using equals() function
        System.out.println("Is LBD1 equal to LBD1: "
                           + LBD1.equals(LBD1));
    }
}
输出:
Linked Blocking Deque 1: [1, 2, 3, 4]
Is LBD1 equal to LBD1: true