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

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

Java中的 BlockingDeque element() 方法及示例

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

句法:

public void element()

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

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

注意BlockingDeque的 element() 方法继承自Java中的 LinkedBlockingDeque 类。

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

方案一:

Java
// Java Program Demonstrate element()
// method of BlockingDeque
 
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of BlockingDeque
        BlockingDeque BD
            = new LinkedBlockingDeque();
 
        // Add numbers to end of BlockingDeque
        BD.add(10);
        BD.add(20);
        BD.add(30);
        BD.add(40);
 
        // before removing print Deque
        System.out.println("Blocking Deque: " + BD);
 
        System.out.println("Blocking Deque front element: " + BD.element());
    }
}


Java
// Java Program Demonstrate element()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
import java.util.concurrent.BlockingDeque;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of BlockingDeque
        BlockingDeque BD
            = new LinkedBlockingDeque();
 
        // Add numbers to end of BlockingDeque
        BD.add("ab");
        BD.add("cd");
        BD.add("fg");
        BD.add("xz");
 
        // before removing print Deque
        System.out.println("Blocking Deque: " + BD);
 
        System.out.println("Blocking Deque front element: " + BD.element());
    }
}


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

方案二:

Java

// Java Program Demonstrate element()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
import java.util.concurrent.BlockingDeque;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of BlockingDeque
        BlockingDeque BD
            = new LinkedBlockingDeque();
 
        // Add numbers to end of BlockingDeque
        BD.add("ab");
        BD.add("cd");
        BD.add("fg");
        BD.add("xz");
 
        // before removing print Deque
        System.out.println("Blocking Deque: " + BD);
 
        System.out.println("Blocking Deque front element: " + BD.element());
    }
}
输出:
Blocking Deque: [ab, cd, fg, xz]
Blocking Deque front element: ab

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