LinkedBlockingDeque的removeFirstOccurrence()方法从此双端队列中删除指定元素的首次出现。如果双端队列不包含元素,则它将保持不变。如果此双端队列包含指定的元素,则返回true,否则返回false。
用法:
public boolean removeFirstOccurrence(Object o)
参数:此方法接受必需的参数o,该参数指定要从Deque容器中删除的元素。
返回值:如果该元素存在并且已从Deque容器中删除,则此方法返回true,否则返回false。
以下示例程序旨在说明LinkedBlockingDeque的removeFirstOccurrence()方法:
示例1:当元素存在时
// 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("Fist 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] Fist occurrence of 15 removed Linked Blocking Deque: [20, 20, 15]
示例2:当元素不存在时
// 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("Fist 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 removed Linked Blocking Deque: [15, 20, 20, 15]
相关用法
- Java LinkedList removeFirstOccurrence()用法及代码示例
- Java ArrayDeque removeFirstOccurrence()用法及代码示例
- Java ConcurrentLinkedDeque removeFirstOccurrence()用法及代码示例
- Java BlockingDeque removeFirstOccurrence()用法及代码示例
- Java LinkedBlockingDeque add()用法及代码示例
- Java LinkedBlockingDeque contains()用法及代码示例
- Java LinkedBlockingDeque put()用法及代码示例
- Java LinkedBlockingDeque pop()用法及代码示例
- Java LinkedBlockingDeque take()用法及代码示例
- Java LinkedBlockingDeque offerLast()用法及代码示例
- Java LinkedBlockingDeque offerFirst()用法及代码示例
- Java LinkedBlockingDeque removeLastOccurrence()用法及代码示例
- Java LinkedBlockingDeque size()用法及代码示例
- Java LinkedBlockingDeque peekLast()用法及代码示例
- Java LinkedBlockingDeque addLast()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 LinkedBlockingDeque removeFirstOccurrence() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。