8

ConcurrentLinkedDeque offer() method in Java with Examples

 3 years ago
source link: https://www.geeksforgeeks.org/concurrentlinkeddeque-offer-method-in-java-with-examples/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ConcurrentLinkedDeque offer() method in Java with Examples
Related Articles
ConcurrentLinkedDeque offer() method in Java with Examples
  • Last Updated : 19 Dec, 2018

The java.util.concurrent.ConcurrentLinkedDeque.offer() method is an inbuilt method in Java which inserts the specified element, passed as a parameter, to the deque.

Syntax:

public boolean offer(E elem)

Parameters: The method accepts a parameter elem which species the element to be inserted to the deque.

Return Value: The function returns True if the element is successfully added into the deque and returns False otherwise.

Exception: The function throws a NullPointerException if the passed parameter is NULL.

Below programs illustrate the ConcurrentLinkedDeque.offer() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Demonstrate offer()
// 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("ConcurrentLinkedDeque: "
+ cld);
// Insert an element using offer()
if (cld.offer("GFG")) {
// Displaying message
System.out.println("Element Inserted");
}
else {
// Displaying message
System.out.println("Element not Inserted");
}
// Displaying the Deque
System.out.println("ConcurrentLinkedDeque: "
+ cld);
}
}
Output:
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
Element Inserted
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks, GFG]

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Demonstrate offer()
// 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("ConcurrentLinkedDeque: "
+ cld);
try {
// Insert null element using offer()
cld.offer(null);
}
catch (Exception e) {
System.out.println(e);
}
}
}
Output:
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#offer-E-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK