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.

The syntax of the containsAll[] method is:

arraylist.containsAll[Collection c];

Here, arraylist is an object of the ArrayList class.

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; }

Check if ArrayList contains a Specific Object

To 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 ArrayList

In 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] { ArrayList arraylist_1 = new ArrayList[ Arrays.asList["apple", "banana", "mango", "orange"]]; String element = "banana"; if[arraylist_1.contains[element]] { System.out.println[element+" is present in the list."]; } else { System.out.println[element+" is not present in the list."]; } } }

Output

banana is present in the list.

Example 2 – Check if Element is present in ArrayList – Negative Scenario

In 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] { ArrayList arraylist_1 = new ArrayList[ Arrays.asList["apple", "banana", "mango", "orange"]]; String element = "lemon"; if[arraylist_1.contains[element]] { System.out.println[element+" is present in the list."]; } else { System.out.println[element+" is not present in the list."]; } } }

As 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.

Conclusion

In 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?


Description:

Here we can see example for finding whether the instance of an ArrayList contains all objects of another Collection instance. Here we are checking with another List instance.


Code:
package com.java2novice.arraylist; import java.util.ArrayList; import java.util.List; public class MyElementCheck { public static void main[String a[]]{ ArrayList arrl = new ArrayList[]; arrl.add["First"]; arrl.add["Second"]; arrl.add["Third"]; arrl.add["Random"]; List list = new ArrayList[]; list.add["Second"]; list.add["Random"]; System.out.println["Does ArrayList contains all list elements?: " +arrl.containsAll[list]]; list.add["one"]; System.out.println["Does ArrayList contains all list elements?: " +arrl.containsAll[list]]; } }

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:

  1. Using contains[] method
  2. Using indexOf[] method

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:

  • object – element whose presence in this list is to be tested

Returns: It returns true if the specified element is found in the list else it returns false.

Java




// Java program to check
// whether element exists
// in Java ArrayList
import java.io.*;
import java.util.ArrayList;
class GFG {
public static void main[String[] args]
{
ArrayList list = new ArrayList[];
// use add[] method to add elements in the list
list.add[1];
list.add[2];
list.add[3];
list.add[4];
// passing 5 as as as
// parameter to contains[]
// function
if [list.contains[5]]
System.out.println["5 exists in the ArrayList"];
else
System.out.println["5 does not exist in the ArrayList"];
if [list.contains[2]]
System.out.println["2 exists in the ArrayList"];
else
System.out.println["2 does not exist in the ArrayList"];
}
}
Output 5 does not exist in the ArrayList 2 exists in the ArrayList

Method 2: Using indexOf[] method

  • Contains[] method uses indexOf[] method to determine if a specified element is present in the list or not.
  • So we can also directly use the indexOf[] method to check the existence of any supplied element value.
Java




// Java program to check
// whether element exists
// in Java ArrayList
import java.io.*;
import java.util.ArrayList;
class GFG {
public static void main [String[] args] {
ArrayList list = new ArrayList[];
// use add[] method to add elements in the list
list.add[2];
list.add[5];
list.add[1];
list.add[6];
// passing 5 as as as
// parameter to contains[]
// function
if[list.indexOf[5]>=0]
System.out.println["5 exists in the ArrayList"];
else
System.out.println["5 does not exist in the ArrayList"];
if[list.indexOf[8]>=0]
System.out.println["8 exists in the ArrayList"];
else
System.out.println["8 does not exist in the ArrayList"];
}
}
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

Video liên quan

Bài mới nhất

Chủ Đề