Java ArrayList.contains() Method
public boolean contains(Object o)
The contains() method is used to determines whether an element exists in an ArrayList object. Returns true if this list contains the specified element.
Package: java.util
Java Platform: Java SE 8
Syntax:
contains(Object o)
Parameters:
Name | Description |
---|---|
o | Element whose presence in this list is to be tested |
Return Value:
true if this list contains the specified element
Return Value Type: boolean
Pictorial presentation of ArrayList.contains() Method
Example: ArrayList.contains Method
The following example checks whether a particular student is present in a list.
import java.util.*;
public class test {
public static void main(String[] args) {
// ArrayList with Capacity 4
ArrayList<String> StudentList = new ArrayList<String>(4);
//Added 4 elements
StudentList.add("David");
StudentList.add("Tom");
StudentList.add("Rohit");
StudentList.add("Paul");
System.out.println("Students in the list are : ");
System.out.println(StudentList);
System.out.print("Is list contains the student Tom?");
System.out.println(StudentList.contains("Tom"));
System.out.print("Is list contains the student Sudhir?");
System.out.println(StudentList.contains("Sudhir"));
}
}
Output:
F:\java>javac test.java F:\java>java test Students in the list are : [David, Tom, Rohit, Paul] Is list contains the student Tom?true Is list contains the student Sudhir?false
Java Code Editor:
Previous:isEmpty Method
Next:indexOf Method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-tutorial/arraylist/arraylist_contains.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics