LinkedBlockingDeque的offerLast(E e)方法将在参数中传递的元素插入Deque容器的末尾。如果超出了容器的容量,则不会像add()和addLast()函数一样返回异常。
用法:
public boolean offerLast(E e)
参数:此方法接受强制参数e,该参数是要在LinkedBlockingDeque末尾插入的元素。
返回值:如果已插入元素,则此方法返回true,否则返回false。
以下示例程序旨在说明LinkedBlockingDeque的offerLast()方法:
示例1:
// Java Program Demonstrate offerLast()
// 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.offerLast(7855642);
LBD.offerLast(35658786);
LBD.offerLast(5278367);
LBD.offerLast(74381793);
// Cannot be inserted
LBD.offerLast(10);
// cannot be inserted hence returns false
if (!LBD.offerLast(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: [7855642, 35658786, 5278367, 74381793]
示例2:
// Java Program Demonstrate offerLast()
// 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.offerLast("abc");
LBD.offerLast("gopu");
LBD.offerLast("geeks");
LBD.offerLast("richik");
// Cannot be inserted
LBD.offerLast("hii");
// cannot be inserted hence returns false
if (!LBD.offerLast("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: [abc, gopu, geeks, richik]
参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#offerLast(E)
相关用法
- Java Deque offerLast()用法及代码示例
- Java ConcurrentLinkedDeque offerLast()用法及代码示例
- Java ArrayDeque offerLast()用法及代码示例
- Java BlockingDeque offerLast()用法及代码示例
- Java LinkedBlockingDeque pop()用法及代码示例
- Java LinkedBlockingDeque take()用法及代码示例
- Java LinkedBlockingDeque add()用法及代码示例
- Java LinkedBlockingDeque put()用法及代码示例
- Java LinkedBlockingDeque contains()用法及代码示例
- Java LinkedBlockingDeque peekFirst()用法及代码示例
- Java LinkedBlockingDeque descendingIterator()用法及代码示例
- Java LinkedBlockingDeque pollLast()用法及代码示例
- Java LinkedBlockingDeque toArray()用法及代码示例
- Java LinkedBlockingDeque removeFirst()用法及代码示例
- Java LinkedBlockingDeque removeLast()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 LinkedBlockingDeque offerLast() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。