这个 removeFirstOccurrence() 方法 LinkedBlockingDeque 从此数据集中删除指定元素的第一个匹配项。如果deque不包含该元素,它将保持不变。如果此数据包含指定的元素,则返回true,否则返回false。 语法:
null
public boolean removeFirstOccurrence(Object o)
参数: 此方法接受一个强制参数 o 它指定要从Deque容器中移除的元素。 返回: 此方法返回 符合事实的 如果元素存在并从Deque容器中移除,则返回 错误的 . 下面的程序演示了LinkedBlockingDeque的RemoveFirstOccurse()方法: 项目1: 当元素存在时
JAVA
// Java Program to demonstrate removeFirstOccurrence() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque LBD.add( 15 ); LBD.add( 20 ); LBD.add( 20 ); LBD.add( 15 ); // print Dequeue System.out.println( "Linked Blocking Deque: " + LBD); if (LBD.removeFirstOccurrence( 15 )) System.out.println( "First occurrence of 15 removed" ); else System.out.println( "15 not present and not removed" ); // prints the Deque after removal System.out.println( "Linked Blocking Deque: " + LBD); } } |
输出
Linked Blocking Deque: [15, 20, 20, 15]First occurrence of 15 removedLinked Blocking Deque: [20, 20, 15]
项目2: 当元素不存在时
JAVA
// Java Program to demonstrate removeFirstOccurrence() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque LBD.add( 15 ); LBD.add( 20 ); LBD.add( 20 ); LBD.add( 15 ); // print Dequeue System.out.println( "Linked Blocking Deque: " + LBD); if (LBD.removeFirstOccurrence( 10 )) System.out.println( "First occurrence of 10 removed" ); else System.out.println( "10 not present and not removed" ); // prints the Deque after removal System.out.println( "Linked Blocking Deque: " + LBD); } } |
输出:
Linked Blocking Deque: [15, 20, 20, 15]10 not present and not removedLinked Blocking Deque: [15, 20, 20, 15]
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END