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

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

Java中的 ConcurrentLinkedDeque removeFirstOccurrence() 方法

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

句法:

public boolean removeFirstOccurrence(Object o)

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

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

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

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

方案一:

/* Java program to demonstrate 
   removeFirstOccurrence() 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 first occurrence of  element
        cld.removeFirstOccurrence("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: [gfg, Gfg, Geeks, GFG]

方案二:

/* Java program to demonstrate 
   removeFirstOccurrence() 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 first occurrence of  element
            cld.removeFirstOccurrence(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#removeFirstOccurrence()