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

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

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

Java .util.concurrent.ConcurrentLinkedDeque .remove()是Java中的一个内置函数,用于从该双端队列中删除一个元素。
句法:

public E remove()
        or
public boolean remove(Object o)

参数:此方法的第一个重载不接受任何参数。但是,此方法的第二个重载接受元素o作为参数,该参数将从该 ConcurrentLinkedDeque 中删除。
返回值:如果没有将特定元素指定为参数,则此方法返回删除的元素。如果一个元素被指定为参数,那么它返回一个布尔值,说明该元素是否被删除。
异常:如果此双端队列为空,则此函数的第一次重载将引发NoSuchElementException 。而如果指定元素为 null,则第二个重载将引发NullPointerException
下面的程序说明了 ConcurrentLinkedDeque.remove() 方法:
示例:1

Java
// Java Program to demonstrate
// ConcurrentLinkedDeque remove() method
 
import java.util.concurrent.*;
 
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
 
        // Create a ConcurrentLinkedDeque
        // using ConcurrentLinkedDeque() constructor
        ConcurrentLinkedDeque
            cld = new ConcurrentLinkedDeque();
 
        cld.add(40);
        cld.add(50);
        cld.add(60);
        cld.add(70);
        cld.add(80);
 
        // Displaying the existing LinkedDeque
        System.out.println("Existing ConcurrentLinkedDeque: "
                           + cld);
 
        // remove method removes the first element of queue
        // using remove() method
        System.out.println("Element removed: "
                           + cld.remove());
 
        // Displaying the existing LinkedDeque
        System.out.println("Modified ConcurrentLinkedDeque: "
                           + cld);
    }
}


Java
// Java Program to demonstrate
// ConcurrentLinkedDeque remove(Object o) method
 
import java.util.concurrent.*;
 
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque cld
            = new ConcurrentLinkedDeque();
 
        cld.add("GFG");
        cld.add("Gfg");
        cld.add("GeeksforGeeks");
        cld.add("Geeks");
 
        // Displaying the existing LinkedDeque
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld);
 
        // Remove "Gfg" using remove(Object)
        System.out.println("Gfg removed: "
                           + cld.remove("Gfg"));
 
        // Displaying the elements
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld);
    }
}


输出:
Existing ConcurrentLinkedDeque: [40, 50, 60, 70, 80]
Element removed: 40
Modified ConcurrentLinkedDeque: [50, 60, 70, 80]

示例:2

Java

// Java Program to demonstrate
// ConcurrentLinkedDeque remove(Object o) method
 
import java.util.concurrent.*;
 
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque cld
            = new ConcurrentLinkedDeque();
 
        cld.add("GFG");
        cld.add("Gfg");
        cld.add("GeeksforGeeks");
        cld.add("Geeks");
 
        // Displaying the existing LinkedDeque
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld);
 
        // Remove "Gfg" using remove(Object)
        System.out.println("Gfg removed: "
                           + cld.remove("Gfg"));
 
        // Displaying the elements
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld);
    }
}
输出:
Elements inthe LinkedDeque: [GFG, Gfg, GeeksforGeeks, Geeks]
Gfg removed: true
Elements inthe LinkedDeque: [GFG, GeeksforGeeks, Geeks]