java.util.concurrent.ConcurrentLinkedDeque.offerFirst()方法是Java中的一种内置方法,它将作为参数传递的指定元素插入双端队列的前面。
用法:
Conn_Linked_Deque.offerFirst(Object elem)
参数:该方法接受参数elem,以选择要插入双端队列的元素的元素。
返回值:如果将元素成功添加到双端队列中,则该函数返回True,否则返回False。
异常:如果传递的参数为NULL,则该函数将引发NullPointerException。
以下示例程序旨在说明ConcurrentLinkedDeque.offerFirst()方法:
程序1:
/* Java Program to Demonstrate offerFirst()
method of ConcurrentLinkedDeque */
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Creating an empty Deque
ConcurrentLinkedDeque<String> cld =
new ConcurrentLinkedDeque<String>();
// Add elements into the Deque
cld.add("Welcome");
cld.add("To");
cld.add("Geeks");
cld.add("4");
cld.add("Geeks");
// Displaying the Deque
System.out.println("Elements in Deque: "
+ cld);
// Displaying the First element
System.out.println("The First element is: " +
cld.getFirst());
/* Insert an element at the front
of the deque */
if (cld.offerFirst("GFG")) {
// Displaying the First element
System.out.println("The Inserted element is: " +
cld.getFirst());
}
// Displaying the Deque
System.out.println("Elements in Deque: "
+ cld);
// Displaying the First element
System.out.println("The First element is: " +
cld.getFirst());
}
}
输出:
Elements in Deque: [Welcome, To, Geeks, 4, Geeks] The First element is: Welcome The Inserted element is: GFG Elements in Deque: [GFG, Welcome, To, Geeks, 4, Geeks] The First element is: GFG
示例2:
/* Java Program to Demonstrate offerFirst()
method of ConcurrentLinkedDeque */
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Creating an empty Deque
ConcurrentLinkedDeque<Integer> cld =
new ConcurrentLinkedDeque<Integer>();
// Add elements into the Deque
cld.add(12);
cld.add(43);
cld.add(29);
cld.add(16);
cld.add(70);
// Displaying the Deque
System.out.println("Elements in Deque: "
+ cld);
// Displaying the First element
System.out.println("The First element is: " +
cld.getFirst());
try {
cld.offerFirst(null);
}
catch (Exception e) {
System.out.println(e);
}
/* Insert an element at the front
of the deque */
if (cld.offerFirst(74)) {
// Displaying the First element
System.out.println("The Inserted element is: " +
cld.getFirst());
}
// Displaying the Deque
System.out.println("Elements in Deque: "
+ cld);
// Displaying the First element
System.out.println("The First element is: " +
cld.getFirst());
}
}
输出:
Elements in Deque: [12, 43, 29, 16, 70] The First element is: 12 java.lang.NullPointerException The Inserted element is: 74 Elements in Deque: [74, 12, 43, 29, 16, 70] The First element is: 74
相关用法
- Java Deque offerFirst()用法及代码示例
- Java LinkedBlockingDeque offerFirst()用法及代码示例
- Java ArrayDeque offerFirst()用法及代码示例
- Java BlockingDeque offerFirst()用法及代码示例
- Java ConcurrentLinkedDeque add()用法及代码示例
- Java ConcurrentLinkedDeque removeFirstOccurrence()用法及代码示例
- Java ConcurrentLinkedDeque peekFirst()用法及代码示例
- Java ConcurrentLinkedDeque getFirst()用法及代码示例
- Java ConcurrentLinkedDeque pollFirst()用法及代码示例
- Java ConcurrentLinkedDeque removeLast()用法及代码示例
- Java ConcurrentLinkedDeque removeFirst()用法及代码示例
- Java ConcurrentLinkedDeque equals()用法及代码示例
- Java ConcurrentLinkedDeque hashCode()用法及代码示例
- Java ConcurrentLinkedDeque peekLast()用法及代码示例
- Java ConcurrentLinkedDeque peek()用法及代码示例
注:本文由纯净天空筛选整理自RICHIK BHATTACHARJEE大神的英文原创作品 ConcurrentLinkedDeque offerFirst() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。