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

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

Java中的 ConcurrentLinkedDeque removeLastOccurrence() 方法

Java.util.concurrent.ConcurrentLinkedDeque.removeLastOccurrence()方法是Java中的一个内置方法,它接受一个参数并删除该元素在双端队列中的最后一次出现。因此,如果元素不存在于双端队列中,则它保持不变。

句法:

public boolean removeLastOccurrence(Object o)

参数:该函数接受一个对象elem作为参数,该参数表示要从双端队列中删除其最后一次出现的对象。

返回值:如果 edeque 中存在elem ,则该函数返回true ,否则返回false

异常:如果作为参数传递给函数的指定元素为 null,则函数将引发NullPointerException

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

/* Java program to demonstrate 
   removeLastOccurrence() method
   of ConcurrentLinkedDeque   */
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque cld = new ConcurrentLinkedDeque();
  
        cld.addFirst("GFG");
        cld.addFirst("Geeks");
        cld.addFirst("Gfg");
        cld.addFirst("gfg");
        cld.addFirst("Geeks");
  
        // Displaying the existing LinkedDeque
        System.out.println("Elements in "
                           + "the LinkedDeque: " + cld);
  
        // Remove last occurrence of  element
        cld.removeLastOccurrence("Geeks");
  
        // Displaying the modified LinkedDeque
        System.out.println("Elements in "
                           + "the LinkedDeque: " + cld);
    }
}
输出:
Elements in the LinkedDeque: [Geeks, gfg, Gfg, Geeks, GFG]
Elements in the LinkedDeque: [Geeks, gfg, Gfg, GFG]

方案二:

/* Java program to demonstrate 
   removeLastOccurrence() method
   of ConcurrentLinkedDeque   */
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque
            cld = new ConcurrentLinkedDeque();
  
        cld.addFirst(12);
        cld.addFirst(280);
        cld.addFirst(1008);
        cld.addFirst(1050);
        cld.addFirst(379);
  
        // Displaying the existing LinkedDeque
        System.out.println("Elements in "
                           + "the LinkedDeque: " + cld);
        try {
            // Remove last occurrence of  element
            cld.removeLastOccurrence(null);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
Elements in the LinkedDeque: [379, 1050, 1008, 280, 12]
java.lang.NullPointerException

参考: https: Java/util/concurrent/ConcurrentLinkedDeque.html#removeLastOccurrence()