Friday, October 24, 2025
HomeLanguagesJavaConcurrentLinkedDeque addFirst() method in Java

ConcurrentLinkedDeque addFirst() method in Java

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

Syntax:

conn_linked_deque.addFirst(elem)

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

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.addFirst() method:

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




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


Output:

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

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




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


Output:

Initial Elements inthe LinkedDeque: [Contribute, Gfg, Geek, Geeks]
NullPointerExceptionthrown
Initial Elements inthe LinkedDeque: [Sudo Placement, Contribute, Gfg, Geek, Geeks]

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

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

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS