Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaConcurrentLinkedDeque addLast() method in Java

ConcurrentLinkedDeque addLast() method in Java

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

Syntax:

conn_linked_deque.addLast(elem)

Parameter: The method accepts only a single parameter elem which is to be added to the end 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 use of java.util.concurrent.ConcurrentLinkedDeque.addLast() method:

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




// Java Program Demonstrate addLast()
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.*;
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        ConcurrentLinkedDeque<Integer> cld = 
                      new ConcurrentLinkedDeque<Integer>();
        cld.addLast(12);
        cld.addLast(110);
        cld.addLast(55);
        cld.addLast(76);
  
        // Displaying the existing LinkedDeque
        System.out.println("Initial Elements in"
                           + "the LinkedDeque: " + cld);
  
        // Insert a new element in the  LinkedDeque
        cld.addLast(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 Integer type with Exception Handling when null is passed as parameter to the function.




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


Output:

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

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

RELATED ARTICLES

Most Popular

Recent Comments