VOOZH about

URL: https://www.geeksforgeeks.org/java/list-contains-method-in-java-with-examples/

⇱ List contains() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

List contains() method in Java with Examples

Last Updated : 3 Dec, 2024

The contains() method of List interface in Java is used for checking if the specified element exists in the given list or not.

Example:


Output
List contains 2 : true
List contains 3 : false


Syntax of Method

public boolean contains(Object obj)

  • Parameters: This method accepts a single parameter obj whose presence in this list is to be tested.
  • Return Value: It returns true if the specified element is found in the list else it returns false.

Practical Application : In search operations, we can check if a given element exists in a list or not.

Example of List contains() Method

Below programs illustrate the contains() method in List:

Program 1: Demonstrate the working of the method contains() in List of integer.


Output
The list contains 2
The list does not contains 5

Program 2: Demonstrate the working of the method contains() in List of string.


Output
The list contains geeks
The list does not contains coding

Reference: Click here to check the official document

Comment