Skip to main content

7. boolean contains(Object o)

7. boolean contains(Object o)
Returns true if this list contains the specified element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.spy.arraylist.method;
 
import java.util.ArrayList;
 
public class ContainMethod 
{
 
	public static void main(String[] args) 
	{
 
        ArrayList<String> emp_list=new ArrayList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
 
 
		for (String s : emp_list) 
		{
 
			System.out.println(s);
		}
 
 
		System.out.println("**************************");
 
		//This method returns true if element present in Arraylist
		boolean b=emp_list.contains("Bhavesh Lakhani");
 
	    if(b)
	    {
	    	System.out.println("Bhavesh Lakhani present in ArrayList");
	    }
 
	    else
	    {
	    	System.out.println("Element not present in Arraylist");
	    }
 
	}
}

Output:

1
2
3
4
5
6
7
Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Vaibhav Prajapati
**************************
 
Bhavesh Lakhani present in ArrayList