當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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<String> cld = new ConcurrentLinkedDeque<String>(); 
  
        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]

示例2:

/* Java program to demonstrate  
   removeLastOccurrence() method 
   of ConcurrentLinkedDeque   */
  
import java.util.concurrent.*; 
  
class ConcurrentLinkedDequeDemo { 
    public static void main(String[] args) 
    { 
        ConcurrentLinkedDeque<Integer> 
            cld = new ConcurrentLinkedDeque<Integer>(); 
  
        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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#removeLastOccurrence()



相關用法


注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 ConcurrentLinkedDeque removeLastOccurrence() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。