LinkedBlockingDeque的removeLastOccurrence()方法從此雙端隊列刪除最後一次出現的指定元素。如果雙端隊列不包含元素,則它將保持不變。如果此雙端隊列包含指定的元素,則返回true,否則返回false。
用法:
public boolean removeLastOccurrence(Object o)
參數:此方法接受強製性參數o,該參數指定最後一次出現的元素將從出隊容器中刪除。
返回值:如果該元素存在並且已從Deque容器中刪除,則此方法返回true,否則返回false。
以下示例程序旨在說明LinkedBlockingDeque的removeLastOccurrence()方法:
示例1:存在元素時。
// Java Program to demonstrate removeLastOccurrence()
// 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.removeLastOccurrence(15))
System.out.println("Last 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] Last occurrence of 15 removed Linked Blocking Deque: [15, 20, 20]
示例2:當元素不存在時。
// Java Program to demonstrate removeLastOccurrence()
// 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.removeLastOccurrence(10))
System.out.println("Last 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 ConcurrentLinkedDeque removeLastOccurrence()用法及代碼示例
- Java LinkedList removeLastOccurrence()用法及代碼示例
- Java ArrayDeque removeLastOccurrence()用法及代碼示例
- Java BlockingDeque removeLastOccurrence()用法及代碼示例
- Java LinkedBlockingDeque add()用法及代碼示例
- Java LinkedBlockingDeque put()用法及代碼示例
- Java LinkedBlockingDeque take()用法及代碼示例
- Java LinkedBlockingDeque contains()用法及代碼示例
- Java LinkedBlockingDeque pop()用法及代碼示例
- Java LinkedBlockingDeque addFirst()用法及代碼示例
- Java LinkedBlockingDeque isEmpty()用法及代碼示例
- Java LinkedBlockingDeque remainingCapacity()用法及代碼示例
- Java LinkedBlockingDeque putLast()用法及代碼示例
- Java LinkedBlockingDeque hashCode()用法及代碼示例
- Java LinkedBlockingDeque putFirst()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LinkedBlockingDeque removeLastOccurrence() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。