Java中的ConcurrentLinkedQue removeLastOccurrence()方法

这个 JAVAutil。同时发生的ConcurrentLinkedEque。Removelastorrence() 方法是Java中的一个内置方法,它接受一个参数并删除该元素在deque中的最后一次出现。因此,如果deque中不存在该元素,它将保持不变。

null

语法:

public boolean removeLastOccurrence(Object o)

参数: 函数接受一个对象 埃伦 作为参数,表示要从deque中删除其最后外观的对象。

返回值: 函数返回 符合事实的 如果 埃伦 在爱德基出现并返回 错误的 否则

例外情况: 函数抛出 空指针异常 如果作为参数传递给函数的指定元素为null。

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

/* 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()

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享