Write a Java program to Searching for elements in an ArrayList


First, the program creates an ArrayList of type String called col_list and adds some color strings to it. Then, the program uses the contains method of ArrayList to search for the string "Pink" and "White" in the list.

The contains method returns true if the element is present in the list and false otherwise. Finally, the program prints out the result of the search using System.out.println.

Source Code

import java.util.ArrayList;
import java.util.List;
public class Searching_Element
{
	public static void main(String[] args) 
	{
		List<String> col_list = new ArrayList<>();
		col_list.add("Blue");
		col_list.add("Green");
		col_list.add("Pink");
		col_list.add("Black");
		col_list.add("Red");
		col_list.add("orange");
		System.out.println("Does Colour array contain \"Pink\" ? : " + col_list.contains("Pink"));
		System.out.println("Does Colour array contain \"White\" ? : " + col_list.contains("White"));
 
	}
}

Output

Does Colour array contain "Pink" ? : true
Does Colour array contain "White" ? : false

Example Programs