BlockingDeque的removeFirstOccurrence()方法從此雙端隊列中刪除指定元素的首次出現。如果雙端隊列不包含元素,則它將保持不變。如果此雙端隊列包含指定的元素,則返回true,否則返回false。
用法:
public boolean removeFirstOccurrence(Object o)
參數:此方法接受必需的參數o,該參數指定要從Deque容器中刪除的元素。
返回值:如果該元素存在並且已從Deque容器中刪除,則此方法返回true,否則返回false。
注意:BlockingDeque的removeFirstOccurrence()方法已從Java中的LinkedBlockingDeque類繼承。
以下示例程序旨在說明BlockingDeque的removeFirstOccurrence()方法:
示例1:當元素存在時
// Java Program to demonstrate removeFirstOccurrence()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.add(15);
BD.add(20);
BD.add(20);
BD.add(15);
// print Deque
System.out.println("Blocking Deque: " + BD);
if (BD.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("Blocking Deque: " + BD);
}
}
輸出:
Blocking Deque: [15, 20, 20, 15] Fist occurrence of 15 removed Blocking Deque: [20, 20, 15]
示例2:當元素不存在時
// Java Program to demonstrate removeFirstOccurrence()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.add(15);
BD.add(20);
BD.add(20);
BD.add(15);
// print Deque
System.out.println("Blocking Deque: " + BD);
if (BD.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("Blocking Deque: " + BD);
}
}
輸出:
Blocking Deque: [15, 20, 20, 15] 10 not present and not removed Blocking Deque: [15, 20, 20, 15]
相關用法
- Java BlockingDeque contains()用法及代碼示例
- Java BlockingDeque add()用法及代碼示例
- Java BlockingDeque take()用法及代碼示例
- Java BlockingDeque put()用法及代碼示例
- Java BlockingDeque addFirst()用法及代碼示例
- Java BlockingDeque push()用法及代碼示例
- Java BlockingDeque offerFirst()用法及代碼示例
- Java BlockingDeque pollLast()用法及代碼示例
- Java BlockingDeque pollFirst()用法及代碼示例
- Java BlockingDeque peek()用法及代碼示例
- Java BlockingDeque offerLast()用法及代碼示例
- Java BlockingDeque element()用法及代碼示例
- Java BlockingDeque poll()用法及代碼示例
- Java BlockingDeque iterator()用法及代碼示例
- Java BlockingDeque putFirst()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 BlockingDeque removeFirstOccurrence() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。