当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。