ConcurrentLinkedDeque offer() method in Java with Examples
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:
// 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); } } |
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks] Element Inserted ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks, GFG]
// 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); } } } |
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-
Recommended Posts:
- BlockingQueue offer() method in Java with examples
- DelayQueue offer() method in Java with Examples
- ConcurrentLinkedDeque pop() method in Java with Examples
- ConcurrentLinkedDeque contains() method in Java with Examples
- ConcurrentLinkedDeque Spliterator() method in Java with Examples
- ConcurrentLinkedDeque remove() method in Java with Examples
- ConcurrentLinkedDeque isEmpty() method in Java with Examples
- ConcurrentLinkedDeque addAll() method in Java with Examples
- ConcurrentLinkedDeque push() method in Java with Examples
- BlockingDeque offer() function in Java with Examples
- ConcurrentLinkedDeque in Java with Examples
- LinkedBlockingQueue | offer() Method in JAVA
- ConcurrentLinkedQueue offer() method in Java
- Queue offer() method in Java
- Deque offer() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


