Does ArrayList contain contains method?

ArrayList contains[] method checks if the list has the specified element or not. Technically, it returns true only when the list contains at least one element where the following condition is met. [o == null ? e== null : e.equals[o]], where e is each element in the list. You can also check the presence of the null element and custom object in the list.

Syntax:

public boolean contains[Object obj]

Parameter obj is the element whose presence needs to be checked.
Returns true if the ArrayList has the specified element; otherwise, it returns false.

ArrayList contains[] Example

Lets look at the program with an Integer type.

import java.util.ArrayList; public class Demo1 { public static void main[String[] args] { ArrayList numbers = new ArrayList[]; numbers.add[12]; numbers.add[43]; numbers.add[54]; numbers.add[23]; numbers.add[89]; numbers.add[76]; System.out.println["ArrayList elements : " + numbers]; System.out.println["Does the list have 54 : " + numbers.contains[54]]; System.out.println["Does the list have 45 : " + numbers.contains[45]]; } }

Output:

ArrayList elements : [12, 43, 54, 23, 89, 76] Does the list have 54 : true Does the list have 45 : false

contains[] with String type

Lets look at the program to see contains[Element e] with String element type. The time complexity of this method is O[n] where n is the number of elements in the list.

import java.util.ArrayList; public class ArrayListStringDemo { public static void main[String[] args] { ArrayList phases = new ArrayList[]; phases.add["Coding"]; phases.add["Testing"]; phases.add["Deploying"]; phases.add["Support"]; System.out.println["ArrayList element: " + phases]; System.out.println["Does the list have Testing: " + phases.contains["Testing"]]; System.out.println["Does the list have Maintainance: " + phases.contains["Maintenance"]]; } }

Output:

ArrayList element: [Coding, Testing, Deploying, Support] Does the list have Testing: true Does the list have Maintainance: false

Using contains with custom object

This program demonstrates how to check the ArrayList contains the custom object. It checks the presence by calling equals[] method of our object.

class Employee { private int id; private String name; public Employee[] { } public Employee[int id, String name] { super[]; this.id = id; this.name = name; } @Override public String toString[] { return "Employee [employeeId=" + id + ", employeeName=" + name + "]"; } @Override public int hashCode[] { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + [[name == null] ? 0 : name.hashCode[]]; return result; } @Override public boolean equals[Object obj] { if [this == obj] return true; if [obj == null] return false; if [getClass[] != obj.getClass[]] return false; Employee other = [Employee] obj; if [id != other.id] return false; if [name == null] { if [other.name != null] return false; } else if [!name.equals[other.name]] return false; return true; } } import java.util.ArrayList; import java.util.List; public class ArrayListDemo { public static void main[String[] args] { List employees = new ArrayList[]; employees.add[new Employee[1, "John"]]; employees.add[new Employee[2, "Stevan"]]; employees.add[new Employee[3, "Thomas"]]; employees.add[new Employee[4, "Brian"]]; Employee employeeToCheck = new Employee[3, "Thomas"]; if[employees.contains[employeeToCheck]] { System.out.println["Brian is present in employees."]; } else { System.out.println["Brian is not present in employees."]; } } }

Thats all about the contains method. You can also go through our articles where we explain how to find the indexOf[] and lastIndexOf[] the element. Please share this article if you liked it.

Further References:

ArrayList Documentation

Video liên quan

Chủ Đề