LinkedBlockingDeque的offerFirst(E e)方法將在參數中傳遞的元素插入Deque容器的前麵。如果超出了容器的容量,則不會像add()和addFirst()函數一樣返回異常。
用法:
public boolean offer(E e)
參數:此方法接受強製參數e,該參數是要在LinkedBlockingDeque的前麵插入的元素。
返回值:如果已插入元素,則此方法返回true,否則返回false。
以下示例程序旨在說明LinkedBlockingDeque的offerFirst()方法:
示例1:
// Java Program Demonstrate offerFirst()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>(4);
// Add numbers to end of LinkedBlockingDeque
LBD.offerFirst(7855642);
LBD.offerFirst(35658786);
LBD.offerFirst(5278367);
LBD.offerFirst(74381793);
// Cannot be inserted
LBD.offerFirst(10);
// cannot be inserted hence returns false
if (!LBD.offerFirst(10))
System.out.println("The element 10 cannot be inserted"+
" as capacity is full");
// before removing print queue
System.out.println("Linked Blocking Deque: " + LBD);
}
}
輸出:
The element 10 cannot be inserted as capacity is full Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]
示例2:
// Java Program Demonstrate offerFirst()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque<String> LBD
= new LinkedBlockingDeque<String>(4);
// Add numbers to end of LinkedBlockingDeque
LBD.offerFirst("abc");
LBD.offerFirst("gopu");
LBD.offerFirst("geeks");
LBD.offerFirst("richik");
// Cannot be inserted
LBD.offerFirst("hii");
// cannot be inserted hence returns false
if (!LBD.offerFirst("hii"))
System.out.println("The element 'hii' cannot be"
+" inserted as capacity is full");
// before removing print queue
System.out.println("Linked Blocking Deque: " + LBD);
}
}
輸出:
The element 'hii' cannot be inserted as capacity is full Linked Blocking Deque: [richik, geeks, gopu, abc]
相關用法
- Java Deque offerFirst()用法及代碼示例
- Java ArrayDeque offerFirst()用法及代碼示例
- Java ConcurrentLinkedDeque offerFirst()用法及代碼示例
- Java BlockingDeque offerFirst()用法及代碼示例
- Java LinkedBlockingDeque add()用法及代碼示例
- Java LinkedBlockingDeque put()用法及代碼示例
- Java LinkedBlockingDeque contains()用法及代碼示例
- Java LinkedBlockingDeque pop()用法及代碼示例
- Java LinkedBlockingDeque take()用法及代碼示例
- Java LinkedBlockingDeque pollLast()用法及代碼示例
- Java LinkedBlockingDeque peekFirst()用法及代碼示例
- Java LinkedBlockingDeque descendingIterator()用法及代碼示例
- Java LinkedBlockingDeque toArray()用法及代碼示例
- Java LinkedBlockingDeque removeFirst()用法及代碼示例
- Java LinkedBlockingDeque removeLast()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LinkedBlockingDeque offerFirst() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。