List contains() method in Java with Examples
The contains() method of List interface in Java is used for checking if the specified element exists in the given list or not.
Syntax:
public boolean contains(Object obj) object-element to be searched forParameters: 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.
Below programs illustrate the contains() method in List:
Program 1: Demonstrate the working of the method contains() in List of integer.
// Java code to demonstrate the working of
// contains() method in List interface
import java.util.*;
class GFG {
public static void main(String[] args)
{
// creating an Empty Integer List
List<Integer> arr = new ArrayList<Integer>(4);
// using add() to initialize values
// [1, 2, 3, 4]
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
// use contains() to check if the element
// 2 exits or not
boolean ans = arr.contains(2);
if (ans)
System.out.println("The list contains 2");
else
System.out.println("The list does not contains 2");
// use contains() to check if the element
// 5 exits or not
ans = arr.contains(5);
if (ans)
System.out.println("The list contains 5");
else
System.out.println("The list does not contains 5");
}
}
|
Program 2: Demonstrate the working of the method contains() in List of string.
// Java code to demonstrate the working of
// contains() method in List of string
import java.util.*;
class GFG {
public static void main(String[] args)
{
// creating an Empty String List
List<String> arr = new ArrayList<String>(4);
// using add() to initialize values
// ["geeks", "for", "geeks"]
arr.add("geeks");
arr.add("for");
arr.add("geeks");
// use contains() to check if the element
// "geeks" exits or not
boolean ans = arr.contains("geeks");
if (ans)
System.out.println("The list contains geeks");
else
System.out.println("The list does not contains geeks");
// use contains() to check if the element
// "coding" exits or not
ans = arr.contains("coding");
if (ans)
System.out.println("The list contains coding");
else
System.out.println("The list does not contains coding");
}
}
|
Practical Application: In search operations, we can check if a given element exists in a list or not.
Reference: //docs.oracle.com/javase/7/docs/api/java/util/List.html#contains(java.lang.Object)
What is the ArrayList.contains() method in Java?
The ArrayList.contains() method in Java is used to check whether or not a list contains a specific element.
To check if an element is in an array, we first need to convert the array into an ArrayList using the asList() method and then apply the same contains() method to it.
1. Overview
Finding an element in a list is a very common task we come across as developers.
In this quick tutorial, we'll cover different ways we can do this with Java.
Java ArrayList.contains() Method
Last update on February 26 2020 08:07:31 (UTC/GMT +8 hours)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:
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?falseJava Code Editor:
Previous:isEmpty Method
Next:indexOf Method
Share this Tutorial / Exercise on : Facebook and Twitter
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework
1. ArrayList contains() syntax
The contains() method is pretty simple. It simply checks the index of element in the list. If the index is greater than '0' than element is present in the list.
public boolean contains(Object o) { return indexOf(o) >= 0; }