The contains() method of CopyOnWriteArrayList in Java


The contains() method of the CopyOnWriteArrayList class is used to get the specified element. It returns TRUE if the element is in the List, else FALSE is returned.

The syntax is as follows

boolean contains(Object ob)

Here, ob is the element to be checked for existence. To work with CopyOnWriteArrayList class, you need to import the following package

import java.util.concurrent.CopyOnWriteArrayList;

The following is an example to implement CopyOnWriteArrayList class contains() method in Java

Example

 Live Demo

import java.util.concurrent.CopyOnWriteArrayList;
public class Demo {
   public static void main(String[] args) {
      CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>();
      arrList.add(100);
      arrList.add(250);
      arrList.add(400);
      arrList.add(500);
      arrList.add(650);
      arrList.add(700);
      arrList.add(800);
      System.out.println("CopyOnWriteArrayList Elements = " + arrList);
      System.out.println("The element at 2nd position = " + arrList.get(1));
      System.out.println("Does the element exist in the List? = " + arrList.contains(400));
   }
}

Output

CopyOnWriteArrayList Elements = [100, 250, 400, 500, 650, 700, 800]
The element at 2nd position = 250
Does the element exist in the List? = true

Updated on: 30-Jul-2019

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements