How do you check if an ArrayList contains another ArrayList?
Java ArrayList containsAll()The Java ArrayList containsAll() method checks whether the arraylist contains all the elements of the specified collection. Show
The syntax of the containsAll() method is: arraylist.containsAll(Collection c);Here, arraylist is an object of the ArrayList class. 1. ArrayList contains() syntaxThe 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; }Check if ArrayList contains a Specific ObjectTo check if ArrayList contains a specific object or element, use ArrayList.contains() method. You can call contains() method on the ArrayList, with the element passed as argument to the method. contains() method returns true if the object is present in the list, else the method returns false. ArrayList.contains() – Reference to Syntax and Examples. Following is a quick code example to use ArrayList.contains() method. boolean b = arraylist_1.contains(element);Example 1 – Check if Element is present in ArrayListIn the following program, we shall take an ArrayList of strings, and check if the string object “banana” is present in the list. Java Program import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main(String[] args) { ArrayListOutput banana is present in the list.Example 2 – Check if Element is present in ArrayList – Negative ScenarioIn the following program, we shall take an ArrayList of strings, and check if the string object “lemon” is present in the list. Java Program import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main(String[] args) { ArrayListAs the specified element is not present in the ArrayList, contains() method returns false, and the else block executes. Output lemon is not present in the list.ConclusionIn this Java Tutorial, we learned how to check if given element is present in the ArrayList or not, using contains() method. Program: How to find does ArrayList contains all list elements or not?
How to Check whether Element Exists in Java ArrayList?Java ArrayList is a resizable array, which can be found in java.util package. We can add or delete elements from an ArrayList whenever we want, unlike a built-in array. We can check whether an element exists in ArrayList in java in two ways:
Method 1: Using contains() method The contains() method of the java.util.ArrayList class can be used to check whether an element exists in Java ArrayList. Syntax: public boolean contains(Object) Parameter:
Returns: It returns true if the specified element is found in the list else it returns false. Java
Output
5 does not exist in the ArrayList
2 exists in the ArrayList
Method 2: Using indexOf() method
Java
Output
5 exists in the ArrayList
8 does not exist in the ArrayList
Article Tags :
Java
Java-ArrayList Java-Collections Practice Tags :
Java Java-Collections
Read Full Article
|