The problemThe 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. Show
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() 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. “how to check if an arraylist of objects contains a value in java” Code Answerjava list contains object with property java by The Red Five on Feb 19 2020 Donate Comment 1 Add a Grepper Answer Java answers related to “how to check if an arraylist of objects contains a value in java”
Java queries related to “how to check if an arraylist of objects contains a value in java”
1. OverviewFinding 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 JavaArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Syntax: Parameters: Returns: Code#1: Demonstrate the working of the method contains() in integer
Output:
The list contains 2
The list does not contains 5
Code#2: Demonstrate the working of the method contains() in string
Output:
The list contains geeks
The list does not contains coding
Practical Application: Reference:
Article Tags :
Java
Java - util package Java-ArrayList Java-Collections java-list Practice Tags :
Java Java-Collections ArrayList contains() MethodBelow 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. |