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

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

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

Java 中的Java Java ()方法用于从 ConcurrentLinkedDeque 中弹出元素。该元素从 ConcurrentLinkedDeque 的顶部弹出并从中删除。
句法:

ConcurrentLinkedDeque.pop()

参数:该方法不带任何参数。
返回值:此方法返回存在于 ConcurrentLinkedDeque 顶部的元素,然后将其移除。
异常:如果 ConcurrentLinkedDeque 为空,则抛出EmptyConcurrentLinkedDequeException方法。
下面的程序说明了Java.util.ConcurrentLinkedDeque.pop() 方法:
方案一:

Java
// Java code to illustrate pop()
 
import java.util.*;
import java.util.concurrent.*;
 
public class ConcurrentLinkedDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ConcurrentLinkedDeque
        ConcurrentLinkedDeque CLD
            = new ConcurrentLinkedDeque();
 
        // Use add() method to add elements
        CLD.push("Welcome");
        CLD.push("To");
        CLD.push("Geeks");
        CLD.push("For");
        CLD.push("Geeks");
 
        // Displaying the ConcurrentLinkedDeque
        System.out.println("Initial ConcurrentLinkedDeque: "
                           + CLD);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " + CLD.pop());
        System.out.println("Popped element: " + CLD.pop());
 
        // Displaying the ConcurrentLinkedDeque after pop operation
        System.out.println("ConcurrentLinkedDeque after pop operation "
                           + CLD);
    }
}


Java
// Java code to illustrate pop()
 
import java.util.*;
import java.util.concurrent.*;
 
public class ConcurrentLinkedDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ConcurrentLinkedDeque
        ConcurrentLinkedDeque CLD
            = new ConcurrentLinkedDeque();
 
        // Use add() method to add elements
        CLD.push(10);
        CLD.push(15);
        CLD.push(30);
        CLD.push(20);
        CLD.push(5);
 
        // Displaying the ConcurrentLinkedDeque
        System.out.println("Initial ConcurrentLinkedDeque: "
                           + CLD);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " + CLD.pop());
        System.out.println("Popped element: " + CLD.pop());
 
        // Displaying the ConcurrentLinkedDeque after pop operation
        System.out.println("ConcurrentLinkedDeque after pop operation "
                           + CLD);
    }
}


输出:

Initial ConcurrentLinkedDeque: [Geeks, For, Geeks, To, Welcome]
Popped element: Geeks
Popped element: For
ConcurrentLinkedDeque after pop operation [Geeks, To, Welcome]

方案二:

Java

// Java code to illustrate pop()
 
import java.util.*;
import java.util.concurrent.*;
 
public class ConcurrentLinkedDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ConcurrentLinkedDeque
        ConcurrentLinkedDeque CLD
            = new ConcurrentLinkedDeque();
 
        // Use add() method to add elements
        CLD.push(10);
        CLD.push(15);
        CLD.push(30);
        CLD.push(20);
        CLD.push(5);
 
        // Displaying the ConcurrentLinkedDeque
        System.out.println("Initial ConcurrentLinkedDeque: "
                           + CLD);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " + CLD.pop());
        System.out.println("Popped element: " + CLD.pop());
 
        // Displaying the ConcurrentLinkedDeque after pop operation
        System.out.println("ConcurrentLinkedDeque after pop operation "
                           + CLD);
    }
}
输出:
Initial ConcurrentLinkedDeque: [5, 20, 30, 15, 10]
Popped element: 5
Popped element: 20
ConcurrentLinkedDeque after pop operation [30, 15, 10]