Check in arraylist java

Java ArrayList contains()

The contains() method checks if the specified element is present in the arraylist.

Example

import java.util.ArrayList; class Main < public static void main(String[] args) < // create an ArrayList ArrayListlanguages = new ArrayList<>(); languages.add("Java"); languages.add("Python"); languages.add("JavaScript"); System.out.println("ArrayList: " + languages); // checks if 3 is present in the arraylist 
System.out.print("Is Java present in the arraylist: "); System.out.println(languages.contains("Java"));
> > // Output: ArrayList: [Java, Python, JavaScript] // Is Java present in the arraylist: true

Syntax of ArrayList contains()

The syntax of the contains() method is:

arraylist.contains(Object obj)

Here, arraylist is an object of the ArrayList class.

contains() Parameter

The contains() method takes a single parameter.

contains() Return Value

  • returns true if the specified element is present in the arraylist.
  • returns false if the specified element is not present in the arraylist.

Example 1: contains() Method with Integer ArrayList

import java.util.ArrayList; class Main < public static void main(String[] args) < // create an ArrayList ArrayListnumbers = new ArrayList<>(); // insert element to the arraylist numbers.add(2); numbers.add(3); numbers.add(5); System.out.println("Number ArrayList: " + numbers); // checks if 3 is present in the arraylist System.out.print("Is 3 present in the arraylist: "); 
System.out.println(numbers.contains(3));
// checks if 1 is present in the arraylist System.out.print("Is 1 present in the arraylist: ");
System.out.println(numbers.contains(1));
> >
Number ArrayList: [2, 3, 5] Is 3 present in the arraylist: true Is 1 present in the arraylist: false

In the above example, we have created an Integer arraylist named number . Notice the expressions,

// returns true number.contains(3) // returns false number.contains(1)

Here, the contains() method checks if 3 is present in the list. Since 3 is present, the method returns true . However, 1 is not present in the list so the method returns false .

Example 2: contains() Method with String ArrayList

import java.util.ArrayList; class Main < public static void main(String[] args) < // create an ArrayList ArrayListlanguages = new ArrayList<>(); // insert element to the arraylist languages.add("Java"); languages.add("JavaScript"); languages.add("Python"); System.out.println("Programming Languages: " + languages); // checks if Java is present in languages System.out.print("Is Java present in the arraylist: "); 
System.out.println(languages.contains("Java"));
// checks if C++ is present in languages System.out.print("Is C++ present in the arraylist: ");
System.out.println(languages.contains("C++"));
> >
Programming Languages: [Java, JavaScript, Python] Is Java present in the arraylist: true Is C++ present in the arraylist: false

In the above example, we have used the contains() method to check if elements Java and C++ are present in the arraylist languages .

Since Java is present in the arraylist, the method returns true . However, C++ is not present in the list. Hence, the method returns false .

Note: The contains() method internally uses the equals() method to find the element. Hence, if the specified element matches with the element in arraylist, the method returns true .

Источник

Check if Element Exists in an ArrayList in Java

The ArrayList.contains() method is used to check whether the specified element exists in the given arraylist. If the element exists, then contains() returns true , else returns false .

Читайте также:  Dynamic reports in java

1. Check if Element Exists using ArrayList.contains()

The contains() method is pretty simple. It simply checks the index of element in the list. If the index is greater than ‘0’ then the element is present in the list.

public boolean contains(Object o) < return indexOf(o) >= 0; >

In the given Java program, we have a few alphabets stored in the arraylist. We will try to find out if letters “A” and “Z” are present in the list or not.

ArrayList list = new ArrayList<>(2); list.add("A"); list.add("B"); list.add("C"); list.add("D"); System.out.println( list.contains("A") ); //true System.out.println( list.contains("Z") ); //false

As noted above, 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.

ArrayList list = new ArrayList<>(2); list.add("A"); list.add("B"); list.add("C"); list.add("D"); System.out.println( list.indexOf("A") >= 0 ); //true System.out.println( list.indexOf("Z") >= 0); //false

Источник

Check if ArrayList contains specified Element

To check if an ArrayList contains specified element in Java, call contains() method on the given ArrayList and pass the element as argument to it.

In this tutorial, we will learn about the Java ArrayList.contains() method, and learn how to use this method to check if this ArrayList contains specified element, with the help of examples.

contains(Object o)

ArrayList.contains() returns true if this list contains the specified element/object. If the element is not present in this ArrayList, then contains() returns false.

Syntax

The syntax of contains() method is

ArrayList.contains(Object obj)
Parameter Description
obj The element whose presence in the ArraList has to be found.

The method returns boolean value.

Example 1 – contains(obj)

In this example, we will define a ArrayList of Strings and add some elements to it. The we shall check if element «c» is present in this ArrayList using contains() method. Since, the element is present in the HashSet, contains() method should return true.

Java Program

import java.util.Arrays; import java.util.ArrayList; public class Example < public static void main(String[] args) < ArrayListarrayList = new ArrayList(Arrays.asList("a", "b", "c")); String element = "c"; boolean result = arrayList.contains(element); System.out.println("ArrayList contains element \"" + element + "\" ? " + result); > >
ArrayList contains element "c" ? true

Example 2 – contains(obj) – Element not present in ArrayList

In this example, we will define a ArrayList of Strings and add some elements to it. The we shall check if element «f» is present in this ArrayList using contains() method. Since, the element is not present in the ArrayList, contains() method should return false.

Java Program

import java.util.Arrays; import java.util.ArrayList; public class Example < public static void main(String[] args) < ArrayListarrayList = new ArrayList(Arrays.asList("a", "b", "c")); String element = "f"; boolean result = arrayList.contains(element); System.out.println("ArrayList contains element \"" + element + "\" ? " + result); > >
ArrayList contains element "f" ? false

Example 3 – contains(obj) – ArrayList with User-defined Objects

In this example, we will define and initialize a ArrayList with objects of type Car . Then we will use contains() method to check if specific Car objects are present in this HashSet.

Java Program

import java.util.ArrayList; public class Example < public static void main(String[] args) < Car car1 = new Car(1, "Tesla"); Car car2 = new Car(2, "BMW"); Car car3 = new Car(3, "Toyota"); ArrayListarrayList = new ArrayList(); arrayList.add(car1); arrayList.add(car2); arrayList.add(car3); boolean result = arrayList.contains(car2); System.out.println("Is car2 present ? " + result); result = arrayList.contains(new Car(2, "BMW")); System.out.println("Is this new Car object present ? " + result); > > class Car < int id; String name; public Car(int id, String name) < this.id = id; this.name = name; >>
Is car2 present ? true Is this new Car object present ? false

In case of arrayList.contains(car2) , we are passing the object instance for checking if it is present. As we have already added car2 to the ArrayList, contains() method returns true.

Читайте также:  If and only if javascript

In case of arrayList.contains(new Car(2, «BMW»)) , we are passing a new object instance for checking if it is present. Since, we have not added this new object instance to ArrayList during initialization, contains() method returns false.

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList.contains() method, and also learnt how to use this method with the help of Java example programs.

Источник

How to Check if ArrayList contains a Specific Object in Java?

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) < ArrayListarraylist_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."); >> >
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) < ArrayListarraylist_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.

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.

Источник

ArrayList contains() Method

The arraylist contains() method is used to check if a specific value exists in an ArrayList or not. In this post, we will learn how to use ArrayList contains() method with examples.

ArrayList contains() Method

Below is the syntax of the contains() method, defined in ArrayList class:

public boolean contains(Object o)

This method takes one object as its parameter. It checks if this object is in the ArrayList or not.It returns one boolean value. If the ArrayList contains at least one element, then it returns true. Else it returns false. It tries to find out at least one element v such that ( o == null ? v == null : o.equals(v) ). i.e. if o is null , it returns true only if one element is null in the ArrayList. If o is not null , it returns true only if at least one element equal to v.

1. ArrayList contains() Example

Let’s look at the below example:

import java.util.ArrayList; import java.util.Arrays; class Main < public static void main(String[] args) < ArrayListarrList = new ArrayList<>(Arrays.asList("Mon", "Tues", "Wed")); if (arrList.contains("Thurs")) < System.out.println("Thurs is in the arraylist"); >else < System.out.println("Thurs is not in the arraylist"); >if (arrList.contains("Tues")) < System.out.println("Tues is in the arraylist"); >else < System.out.println("Tues is not in the arraylist"); >> >

We have created one ArrayList of strings in this example. The first if-else block checks if the string “ Thurs ” is in that ArrayList, and the second if-else block checks if “ Tues ” is in the ArrayList. If you run this program, it will print the below output:

Thurs is not in the arraylist Tues is in the arraylist

We can use ArrayList contains() method that holds any type of data. For example, the below example shows how it works with an ArrayList of integers:

import java.util.ArrayList; import java.util.Arrays; class Main < public static void main(String[] args) < ArrayListarrList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); if (arrList.contains(5)) < System.out.println("5 is in the arraylist"); >else < System.out.println("5 is not in the arraylist"); >> >

The output will be same as the previous example. As mentioned before, contains can check for null values in an ArrayList. For example:

import java.util.ArrayList; import java.util.Arrays; class Main < public static void main(String[] args) < ArrayListarrList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, null)); if (arrList.contains(null)) < System.out.println("null is in the arraylist"); >else < System.out.println("null is not in the arraylist"); >> >

This program is checking for null values in the ArrayList. It will print “null is in the arraylist” since we have one null value. This method comes in handy to check for null values quickly in an ArrayList.

2. ArrayList indexOf() example

The contains method is useful to check if any specific element is available in an ArrayList or not. But if we want the index of the first occurrence of that element in the ArrayList, we can use indexOf . We can also use this method to check if any value is available or not in an ArrayList. We define it as below:

public int indexOf(Object o)

It takes one parameter, which is the element to search for. The return value is an integer. It returns -1 if we find no element equal to the provided element. Else, it returns the index of the first occurrence of the specific element.Let’s try it with an example:

import java.util.ArrayList; import java.util.Arrays; class Main < public static void main(String[] args) < ArrayListarrList = new ArrayList<>(Arrays.asList(1, 2, 3, 1, 4, 5, 6, 7)); System.out.println(arrList.indexOf(1)); System.out.println(arrList.indexOf(10)); > >

The first print statement is printing the index of 1 in the given ArrayList, and the second print statement is printing the index of 10 in that ArrayList. If you run it, it will print the below output:

Читайте также:  What does java program do

1 appears two times in the ArrayList, but it will return the index of its first occurrence, i.e. 0 . 10 is not in the ArrayList, so it returns -1 . If we need to check if an element is in an ArrayList or not and also its index, we can use indexOf . -1 return value from this method is equal to false return from contains().

Summary

We learned how to use ArrayList contains() method and indexOf method of Java ArrayList in this post. We can use both methods to check if an element is in an ArrayList or not. indexOf also returns the index of an element, which makes it easier to get the position of an element if an ArrayList holds only unique values. The source code for these articles is available on our GitHub repository.

Источник

Оцените статью