Java

What is the ArrayList.contains() method in Java

While programming in Java, there can be instances where the developer needs to locate the entries in the case of bulk data. For instance, accessing vital data or a part of it to utilize it instantly. In such situations, the “ArrayList.contains()” method in Java is of great aid in accessing, analyzing, and updating the current resources effectively.

This blog will elaborate on the usage of the “ArrayList.contains()” method in Java.

What is the “ArrayList.contains()” Method in Java?

The “ArrayList.contains()” method in Java is used to check if the specified element is contained in the provided list or not.

Syntax

public boolean contains(Object)

In the above syntax, “Object” refers to the object element that needs to be searched.

Example 1: Utilization of “ArrayList.contains()” Method to Apply a Check Upon Integer Array List

In this example, the “ArrayList.contains()” method can be used to add integers in an ArrayList and then check if a particular integer is contained in the list:

ArrayList<Integer> numArr = new ArrayList<>();

numArr.add(5);

numArr.add(6);

numArr.add(7);

System.out.println("Integer ArrayList is: " + numArr);

System.out.print("Is 5 present in the arraylist? ");

System.out.println(numArr.contains(5));

System.out.print("Is 1 present in the arraylist? ");

System.out.println(numArr.contains(1));

In the above code snippet:

  • Firstly, create a new “ArrayList” object comprising of integers.
  • In the next step, apply the “add()” method to add the specified integers as its(method) parameter into an array and display it.
  • After that, associate the “contains()” method with the specified ArrayList to check if the specified integers are contained in the ArrayList.
  • If so, the method returns a boolean value “true”. Otherwise, “false” is returned.

Output

In the above output, it can be seen that the former integer is contained in the “ArrayList” whereas it is not the case in the latter one.

Example 2: Utilization of “ArrayList.contains()” Method to Apply a Check Upon String Array List

In this particular illustration, the “ArrayList.contains()” method can be utilized to check for the contained string in the “ArrayList”:

ArrayList<String> strArr = new ArrayList<>();

strArr.add("Harry");

strArr.add("David");

strArr.add("Albert");

System.out.println("String ArrayList is: " + strArr);

System.out.print("Is Jacob present in the arraylist? ");

System.out.println(strArr.contains("Jacob"));

System.out.print("Is David present in the arraylist? ");

System.out.println(strArr.contains("David"));

Apply the following steps as given in the above lines of code:

  • Likewise, recall the discussed steps for creating a new “ArrayList” object and adding the stated string values.
  • After that, associate the “contains()” method to check for the specified strings in the ArrayList and display the corresponding boolean values upon the satisfied and unsatisfied condition.

Output

Example 3: Utilization of “ArrayList.contains()” Method to Apply a Check Upon Both the Integer and String ArrayList Values

In this particular example, the discussed method can be implemented to apply a check upon both the integer and string values of the array list:

ArrayList<Object> strintArr = new ArrayList<>();

strintArr.add("Harry");

strintArr.add(1);

strintArr.add("Albert");

System.out.println("The ArrayList is: " + strintArr);

System.out.print("Is 1 present in the arraylist? ");

System.out.println(strintArr.contains(1));

System.out.print("Is David present in the arraylist? ");

System.out.println(strintArr.contains("David"));

In the above illustration, perform the following steps:

  • Likewise, create a new “ArrayList” object.
  • Note that “Object” supports both the string and integer values in an array.
  • After that, repeat the discussed steps to append the stated string and integer values into “ArrayList”.
  • Finally, check for the particular integer and string values in the ArrayList via the “contains()” method and return the corresponding output.

Output

This output signifies that the desired requirement is fulfilled.

Conclusion

The “ArrayList.contains()” method in Java is utilized to verify if the specified element is contained in the provided list or not. This method can be applied to check for the integer and string in the associated ArrayList and returns the corresponding outcome in the form of a boolean value. This article illustrated the usage of the “ArrayList.contains()” method in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.