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
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:
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”:
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:
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.