Java Arraylist contains() Function

contains function in java

What is an ArrayList?

Arraylist is a part of collection framework in java. It contains some inbuilt function like contains function which can be found in the java.util.ArrayList Package.
It implements the List interface of the collections framework.

Here, in the page we will discuss about the ArrayList’s contains() Method in java.

ArrayList contains Function:

Arraylist contains several inbuilt functions that are used to perform several operations in of  arraylist in Java. contains() function is also an inbuilt method of class ArrayList.
contains() method is used to checks whether the given element is present in the arraylist or not.

Syntax:

arraylist_name.contains(element)

Definition of Parameters:

Return Type :

Example:

clear() Function in ArrayList

clone() Function in ArrayList

add() Function in ArrayList

addall() Function in ArrayList

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {
    // Initializing the arraylist
    ArrayList PrepInsta = new ArrayList<>();
    
    // Adding Elements to the ArrayList
    PrepInsta.add(10);
    PrepInsta.add(20);
    PrepInsta.add(30);

    // Printing the ArrayList
    System.out.println("ArrayList: " + PrepInsta);
    
    // Contains Function for true value
    System.out.println("Is 10 Present in the ArrayList : " + PrepInsta.contains(20));
    
    // Contains Function for false value
    System.out.println("Is 40 Present in the ArrayList : " + PrepInsta.contains(40));
  }
}
Output:

ArrayList: [10, 20, 30]
Is 10 Present in the ArrayList : true
Is 40 Present in the ArrayList : false

In the above code we had taken an integer ArrayList with some integer elements, then we checked for the elements ’10’ and ’40’ in the ArrayList whether they are present in the given ArrayList or not. It returns true for 10 and false for 40.

Example for Different Data Type:

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {
    // Initializing the arraylist
    ArrayList PrepInsta = new ArrayList<>();
    
    // Adding Elements to the ArrayList
    PrepInsta.add("Lakshit");
    PrepInsta.add("Rishikesh");
    PrepInsta.add("Rishik");

    // Printing the ArrayList
    System.out.println("ArrayList: " + PrepInsta);
    
    // Contains Function for true value
    System.out.println("Is Lakshit Present in the ArrayList : " 
    + PrepInsta.contains("Lakshit"));
    
    // Contains Function for false value
    System.out.println("Is Insta Present in the ArrayList : " 
    + PrepInsta.contains("Insta"));
  }
}
Output:

ArrayList: [Lakshit, Rishikesh, Rishik]
Is Lakshit Present in the ArrayList : true
Is Insta Present in the ArrayList : false

In the above Example. we add 3 elements to an arraylist of string type and we are searching for the elements for one true and one false value.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription