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

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

Java中的 LinkedBlockingDeque element() 方法

LinkedBlockingDequeelement()方法返回容器前面的元素。它不会删除容器中的元素。此方法返回此双端队列表示的队列的头部。

句法:

public void element()

参数:此方法不接受任何参数。

返回:此方法返回此双端队列表示的队列的头部。

下面的程序说明了 LinkedBlockingDeque 的 element() 方法:

方案一:

Java
// Java Program Demonstrate element()
// 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 LBD
            = new LinkedBlockingDeque();
 
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(10);
        LBD.add(20);
        LBD.add(30);
        LBD.add(40);
 
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
 
        System.out.println("Linked Blocking Deque front element: " +
                                                     LBD.element());
    }
}


Java
// Java Program Demonstrate element()
// 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 LBD
            = new LinkedBlockingDeque();
 
        // Add numbers to end of LinkedBlockingDeque
        LBD.add("ab");
        LBD.add("cd");
        LBD.add("fg");
        LBD.add("xz");
 
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
 
        System.out.println("Linked Blocking Deque front element: " +
                                                     LBD.element());
    }
}


输出:
Linked Blocking Deque: [10, 20, 30, 40]
Linked Blocking Deque front element: 10




方案二:

Java

// Java Program Demonstrate element()
// 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 LBD
            = new LinkedBlockingDeque();
 
        // Add numbers to end of LinkedBlockingDeque
        LBD.add("ab");
        LBD.add("cd");
        LBD.add("fg");
        LBD.add("xz");
 
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
 
        System.out.println("Linked Blocking Deque front element: " +
                                                     LBD.element());
    }
}
输出:
Linked Blocking Deque: [ab, cd, fg, xz]
Linked Blocking Deque front element: ab




参考: https: Java/util/concurrent/LinkedBlockingDeque.html#element()