📌  相关文章
📜  Java中的 ConcurrentLinkedDeque element() 方法

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

Java中的 ConcurrentLinkedDeque element() 方法

Java .util.concurrent.ConcurrentLinkedDeque .element()是Java中的一个内置函数,它检索但不删除由deque 表示的队列的头部,即deque 的第一个元素。
句法:

conn_linked_deque.element()

参数:此方法没有参数。
返回值:此方法返回双端队列中的第一个元素
异常:如果双端队列为空,此方法将抛出NoSuchElementException
下面的程序说明了 ConcurrentLinkedDeque.element() 方法:
方案 1:该方案涉及 Integer 类型的 ConcurrentLinkedDeque。

JAVA
// Java example illustrating
// ConcurrentLinkedDeque element() 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(12);
        cld.add(70);
        cld.add(1009);
        cld.add(475);
 
        // Displaying the existing LinkedDeque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
 
        // Displaying the head of deque
        System.out.println("The Head of deque is: "
                           + cld.element());
    }
}


JAVA
// Java example illustrating
// ConcurrentLinkedDeque element() 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("Gfg");
        cld.add("Geeks");
        cld.add("Programming");
        cld.add("contribute");
 
        // Displaying the existing LinkedDeque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
 
        // Displaying the head of deque
        System.out.println("The Head of deque is: "
                           + cld.element());
    }
}


输出:
ConcurrentLinkedDeque: [12, 70, 1009, 475]
The Head of deque is: 12

方案2:这个方案涉及到一个String类型的ConcurrentLinkedDeque。

Java

// Java example illustrating
// ConcurrentLinkedDeque element() 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("Gfg");
        cld.add("Geeks");
        cld.add("Programming");
        cld.add("contribute");
 
        // Displaying the existing LinkedDeque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
 
        // Displaying the head of deque
        System.out.println("The Head of deque is: "
                           + cld.element());
    }
}
输出:
ConcurrentLinkedDeque: [Gfg, Geeks, Programming, contribute]
The Head of deque is: Gfg