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

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

Java中的 LinkedBlockingDeque removeLastOccurrence() 方法

LinkedBlockingDequeremoveLastOccurrence()方法从该双端队列中移除指定元素的最后一次出现。如果双端队列不包含该元素,则它保持不变。如果此双端队列包含指定的元素,则返回 true,否则返回 false。

句法:

public boolean removeLastOccurrence(Object o)

参数:此方法接受一个强制参数o ,该参数指定要从 Dequeue 容器中删除最后一次出现的元素。

返回:如果元素存在并从 Deque 容器中删除,则此方法返回true ,否则返回false

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

程序 1:当元素存在时。

// Java Program to demonstrate removeLastOccurrence()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(15);
        LBD.add(20);
        LBD.add(20);
        LBD.add(15);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
  
        if (LBD.removeLastOccurrence(15))
            System.out.println("Last occurrence of 15 removed");
        else
            System.out.println("15 not present and not removed");
  
        // prints the Deque after removal
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}
输出:
Linked Blocking Deque: [15, 20, 20, 15]
Last occurrence of 15 removed
Linked Blocking Deque: [15, 20, 20]

程序 2:当元素不存在时。

// Java Program to demonstrate removeLastOccurrence()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(15);
        LBD.add(20);
        LBD.add(20);
        LBD.add(15);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
  
        if (LBD.removeLastOccurrence(10))
            System.out.println("Last occurrence of 10 removed");
        else
            System.out.println("10 not present and not removed");
  
        // prints the Deque after removal
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}
输出:
Linked Blocking Deque: [15, 20, 20, 15]
10 not present and not removed
Linked Blocking Deque: [15, 20, 20, 15]

参考: https: Java/util/concurrent/LinkedBlockingDeque.html#removeLastOccurrence-java.lang.Object-