Skip to content
Related Articles

Related Articles

Improve Article

CopyOnWriteArrayList addIfAbsent() method in Java

  • Last Updated : 26 Nov, 2018

The addIfAbsent(E e) method of CopyOnWriteArrayList appends the element passed in the parameter to the end of the List if the element is not present in the list. The function returns true on addition of new element to the list.

Syntax:

public boolean addIfAbsent(E e)

Parameters: The function accepts a single mandatory parameter element which specifies the element to be appended to the list if it is not present.

Return Value: The function returns true if it is added to the list.

Below programs illustrate the above function:



Program 1:




// Java Program to illustrate the CopyOnWriteArrayList
// addIfAbsent(element) method in Java
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis.addIfAbsent(2);
        ArrLis.addIfAbsent(3);
        ArrLis.addIfAbsent(4);
        ArrLis.addIfAbsent(7);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: " 
                                           + ArrLis);
  
        System.out.println("On adding 4 it returns " 
          + ArrLis.addIfAbsent(4) + " as it is present");
    }
}
Output:
CopyOnWriteArrayList: [2, 3, 4, 7]
On adding 4 it returns false as it is present

Program 2:




// Java Program to illustrate the CopyOnWriteArrayList
// addIfAbsent(element) method in Java
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<String> ArrLis
            = new CopyOnWriteArrayList<String>();
  
        // Add elements
        ArrLis.addIfAbsent("gopal");
        ArrLis.addIfAbsent("gfg");
        ArrLis.addIfAbsent("jgec");
        ArrLis.addIfAbsent("sudo");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: " + ArrLis);
  
        System.out.println("On adding 'sudo' it returns " 
            + ArrLis.addIfAbsent("sudo") + " as it is present");
    }
}
Output:
CopyOnWriteArrayList: [gopal, gfg, jgec, sudo]
On adding 'sudo' it returns false as it is present

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#addIfAbsent-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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :