Thursday, September 4, 2025
HomeLanguagesJavaConcurrentLinkedDeque add() method in Java

ConcurrentLinkedDeque add() method in Java

The java.util.concurrent.ConcurrentLinkedDeque.add() is an in-built function in Java which inserts the specified element at the end of the deque.

Syntax:

conn_linked_deque.add(elem)

Parameter: The method accepts only a single parameter elem which is to be added to tail of the ConcurentLinkedDeque.

Return Value: The function has no return value.

Exception:The method will throw NullPointerException when the parameter passed to the function is null. Due to its bounded nature, this method will never throw IllegalStateException or return false.

Below programs illustrate the ConcurrentLinkedDeque.add() method:

Program 1: This program involves a ConcurrentLinkedDeque of Integer type.




// Java Program Demonstrate add()
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque<Integer> cld = 
                     new ConcurrentLinkedDeque<Integer>();
  
        cld.add(12);
        cld.add(110);
        cld.add(55);
        cld.add(76);
  
        // Displaying the existing LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
  
        // Insert a new element in the  LinkedDeque
        cld.add(21);
  
        // Displaying the modified LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
    }
}


Output:

Initial Elements inthe LinkedDeque: [12, 110, 55, 76]
Initial Elements inthe LinkedDeque: [12, 110, 55, 76, 21]

Program 2: This program involves a ConcurrentLinkedDeque of String type with Exception Handling when null is passed as parameter to the function.




// Java Program Demonstrate add()
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque<String> cld = 
                       new ConcurrentLinkedDeque<String>();
  
        cld.add("Gfg");
        cld.add("GFG");
        cld.add("Geeksforgeeks");
        cld.add("Contribute");
  
        // Displaying the existing LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
  
        /* Exception thrown when null 
             is passed as parameter*/
        try {
            cld.add(null);
        }
        catch (NullPointerException e) {
            System.out.println("NullPointerException"
                               + "thrown");
        }
  
        // Insert a new element in the  LinkedDeque
        cld.add("Geek Classes");
        // Displaying the modified LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
    }
}


Output:

Initial Elements inthe LinkedDeque: [Gfg, GFG, Geeksforgeeks, Contribute]
NullPointerExceptionthrown
Initial Elements inthe LinkedDeque: [Gfg, GFG, Geeksforgeeks, Contribute, Geek Classes]

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#add()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS