How do you check if an ArrayList of objects contains a value in Java?

The problem

The reason why your approach did not work is because the List#contains (documentation) method uses the result of Person#equals to decide if an element from the list is the same as the argument. If you have not implemented this method than it searches the method in the inheritance tree of Person. As every object inherits from the class Object you will fall back to Object#equals (documentation) if no class on the way to Object has implemented equals. However this Object#equals method compairs objects by their identity (place in the memory). That means that if you create an object that has the same properties (like name and so on) it just will be a copy but not the same as the one in the list, per identity.

Note that there are many questions here at StackOverflow for this topic. If you're not familiar with it you should check it out. Here is an example explaining it in more detail: What issues should be considered when overriding equals and hashCode in Java?

There are multiple ways of solving this problem. Let's first consider a very straightforward and easy way.

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

“how to check if an arraylist of objects contains a value in java” Code Answer


java list contains object with property
java by The Red Five on Feb 19 2020 Donate Comment
1
Add a Grepper Answer

  • java find if element of list in present in another list
  • check if a list contains a string java
  • java check if string appears twice in arraylist
  • check if object in array java
  • arraylistof objects

  • java check if list contains object with property
  • java list contains object
  • java list contains object with property
  • how to check if an objects has certain values in java
  • java contains object in list
  • list object contains something
  • list contains in list of object
  • contains(object list) java
  • how to check if an arraylist of objects contains a value in java
  • list contain object by value
  • condition check from a arraylist field
  • find a specific object in arraylist in java and return a boolean
  • andori java containts object with string
  • contains in list of objects in java
  • java check if object in a list contains property stream
  • java 7 list contains object attribute
  • list contains matching all fields
  • contains in list of object java
  • list object contains equals
  • java list of object contains value
  • java 8 check if list of objects contains objects with same value
  • how to check list contains model object in java
  • java list of object contains value by field
  • java contains list object
  • list contains object
  • list contains object java
  • check if an object has a property in a list java
  • java check if any object in list satisfies property
  • java check if object is in list based on attribute
  • check if object in list java
  • what contained in an object java
  • java in list of list object find the list based on value of object
  • list contains complex class java
  • java check if dto contains attributes of an object
  • stream list verify item atributes java
  • find jeck if a particular field is present in java dto java 8
  • checking if a list object is equal to a spesoific string java
  • java get item in object
  • how to check if list contains object java
  • check if a list contains an object java
  • arraylist of object java if
  • list object contains java
  • c# object list contains object
  • using filter and stream to check if object is in list java
  • how to find value in arraylist java
  • contains predicate java
  • java check if arraylist contains objects with same property value
  • check if object property is equal android
  • list contains object value java
  • java list contains for item of object in list
  • java list has value
  • java 8 check if list contains object with property
  • java list of objects contains some atribute
  • how to check if an list of object contains an object android java
  • list if has object with value
  • check if list contains object with property java
  • java array check if list contains object with property
  • java check if array contains object with property
  • java check if list contains element with property
  • get value in list where name is equal to java
  • java array contains object with property
  • java check if list contains object with attribute
  • check if a field in a list contains a variable
  • java if list contains object
  • how to use contains in collection of objects
  • java list contains based on property
  • java how to check if a list contains an object
  • java if list contains object with value
  • java check if list contains custom object
  • java check if object with properties exists in list of objects
  • check if an array list of objects contains a particular object java
  • java 8 check if list of objects contains objects with same name
  • java check if list of objects contains object with value
  • check object exist in list java 8
  • java 8 check if list contains object with property code example
  • contains object inside list java
  • how to check object contains in list in java
  • checking if a list of objects contains a object's attributes java
  • check if object is present in list by property java
  • java list contains object of type
  • android check if list contain object
  • how to find list contains value from object in java
  • list.contains object o
  • java 8 list::contains
  • how to checking the list of objects contains specific value java
  • list contains object with property java
  • check stream is object contains value java
  • checking if a string is equal to a object param in a lsit java
  • using array contains cloud_firestore
  • java check var contains object
  • how to check a particular field of object from list of objects
  • android list contains object with key
  • specific element of list in java
  • get elements that don't exist in a list java
  • find string in list java
  • java get element list
  • check if list contains object
  • java list contains object with property value
  • arraylist contains object with value
  • check if list contains object with same values android
  • java check if arraylist has an object that has a value
  • search object property contains value in java arraylist
  • java check if list contains(object with property)
  • java check if list of objects contains value
  • list contains java object
  • java list contains object with value
  • how to check if a list contains an object java
  • list contains object in java
  • java check if list of objects contains one type
  • from list of classes find a object eith specific field
  • check if every object in a list as a property java
  • java find single element from list that contains 2 elements
  • java list contains object attribute
  • java array contains for a property of object
  • how to check if an list of object contains an object java
  • java list of objects .contains not working
  • check if list of objects contains value java
  • check if a list contains an certasin object in java
  • java check objects the list contains
  • contains method list java with object
  • arraylist contains object attribute java
  • java list contain object
  • list contains objects
  • check object is in list java
  • java how to check for a property in list of object
  • java check list of object contains value
  • check object not include list java
  • java object contains in list
  • java arraylist contains object property
  • check if list contains object has value java
  • java list<object> contains new object
  • java arraylist contains object whit specific value
  • how to check list contains value from object in java
  • java predicate list contains
  • java list of objects contains object
  • how to check list contains object in java
  • how to check if an arraylist contains a value in java 8
  • condition check from a object type arraylist field
  • how to check a list of object by attribute java
  • list has value c#
  • any object in list has value
  • java check if an object is equal to something in a list
  • contains member java
  • how to check if a value matches any one of an object's value in java
  • find object in set that has the same value for datafield
  • java list of object contains obj with key
  • java check list contains object
  • list.cotains can't find java
  • java test list contain list of field
  • how to check if a list contains an object in java
  • .contains with object list android
  • check if object list contains value java

1. Overview

Finding an element in a list is a very common task we come across as developers.

In this quick tutorial, we'll cover different ways we can do this with Java.

Arraylist.contains() in Java

ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not.

Syntax:

public boolean contains(Object) object-element to be searched for

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

Code#1: Demonstrate the working of the method contains() in integer






// Java code to demonstrate the working of
// contains() method in ArrayList
// for ArrayList functions
import java.util.ArrayList;
class GFG {
public static void main(String[] args)
{
// creating an Empty Integer ArrayList
ArrayList<Integer> arr = new ArrayList<Integer>(4);
// using add() to initialize values
// [1, 2, 3, 4]
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
// use contains() to check if the element
// 2 exits or not
boolean ans = arr.contains(2);
if (ans)
System.out.println("The list contains 2");
else
System.out.println("The list does not contains 2");
// use contains() to check if the element
// 5 exits or not
ans = arr.contains(5);
if (ans)
System.out.println("The list contains 5");
else
System.out.println("The list does not contains 5");
}
}
Output: The list contains 2 The list does not contains 5

Code#2: Demonstrate the working of the method contains() in string




// Java code to demonstrate the working of
// contains() method in ArrayList of string
// for ArrayList functions
import java.util.ArrayList;
class GFG {
public static void main(String[] args)
{
// creating an Empty String ArrayList
ArrayList<String> arr = new ArrayList<String>(4);
// using add() to initialize values
// ["geeks", "for", "geeks"]
arr.add("geeks");
arr.add("for");
arr.add("geeks");
// use contains() to check if the element
// "geeks" exits or not
boolean ans = arr.contains("geeks");
if (ans)
System.out.println("The list contains geeks");
else
System.out.println("The list does not contains geeks");
// use contains() to check if the element
// "coding" exits or not
ans = arr.contains("coding");
if (ans)
System.out.println("The list contains coding");
else
System.out.println("The list does not contains coding");
}
}
Output: The list contains geeks The list does not contains coding

Practical Application:
In search operations, we can check if a given element exists in a list or not.

Reference:
Oracle Docs

How do you check if an ArrayList of objects contains a value in Java?




Article Tags :
Java
Java - util package
Java-ArrayList
Java-Collections
java-list
Practice Tags :
Java
Java-Collections

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 vsuch that (o == null ? v == null : o.equals(v)). i.e. if ois null,it returns trueonly if one element isnull in the ArrayList. If ois not null, it returns trueonly if at least one element equal to v.