You are right. ArrayList.contains() tests equals(), not object identity:

returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))

If you got a NullPointerException, verify that you initialized your list, either in a constructor or the declaration. For example:

private List<String> rssFeedURLs = new ArrayList<String>();
Answer from Andy Thomas on Stack Overflow
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ ArrayList.html
ArrayList (Java Platform SE 8 )
July 21, 2026 - Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. ... Returns the number of elements in this list. ... Returns true if this list contains no elements.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_contains.asp
Java ArrayList contains() Method
The contains() method returns true if an item exists in a list and false otherwise.
Discussions

Use of contains in Java ArrayList<String> - Stack Overflow
If I have an ArrayList of String forming part of a class in Java like so: private ArrayList rssFeedURLs; If I want to use a method in the class containing the above ArrayList, using More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I know if an array list contains an specific string
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
14
9
May 12, 2022
java - Using contains on an ArrayList with integer arrays - Stack Overflow
If you need to check for containment within the Array then you are left with no choice but to iterate through the ArrayList and do the comparison yourself. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to use the ArrayList.contain() to check the object in java - Stack Overflow
I am practicing the ArrayList with contains method. For checking the objects in the ArrayList, every time I need to loop through the entire ArrayList using for each loop. Therefore, I am thinking if More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arraylist-contains-java
Arraylist.contains() Method in Java - GeeksforGeeks
July 18, 2026 - The contains() method returns false for "Hari" since it is not in the list and true for "Ram" because it is present. Quickly checks whether an element exists in the list. Returns a simple boolean result.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-arraylistcontains-method-in-java
What is the ArrayList.contains() method in Java?
The ArrayList.contains() method in Java is used to check whether or not a list contains a specific element.
Find elsewhere
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ performance of contains() in a hashset vs arraylist
Performance of contains() in a HashSet vs ArrayList | Baeldung
April 4, 2025 - This is an improvement from Java 7 which used a LinkedList for the internal bucket structure. In general, hash code collisions are rare. So we can consider the elements lookup complexity as O(1). Internally, ArrayList uses the indexOf(object) method to check if the object is in the list. The indexOf(object) method iterates the entire array and compares each element with the equals(object) method. Getting back to complexity analysis, the ArrayList.contains() method requires O(n) time.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ arraylist โ€บ contains
Java ArrayList contains()
The contains() method takes a single parameter. obj - element that is checked if present in the arraylist ยท returns true if the specified element is present in the arraylist. returns false if the specified element is not present in the arraylist. import java.util.ArrayList; class Main { public ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java โ€บ util โ€บ arraylist_contains.htm
Java ArrayList contains() Method
package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // create an empty arrayList ArrayList<Integer> arrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add(20); arrayList.add(30); arrayList.add(10); arrayList.add(18); // let us print all the elements available in arrayList System.out.println("ArrayList = " + arrayList); // arrayList contains element 10 if (arrayList.contains(10)) { System.out.println("element 10 is present in the arrayList"); } else { System.out.println("element 10 is not present in the arrayList"); } // arrayList does not contain element 25 if (arrayList.contains(25)) { System.out.println("element 25 is present in the arrayList"); } else { System.out.println("element 25 is not present in the arrayList"); } } }
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how do i know if an array list contains an specific string
r/javahelp on Reddit: How do I know if an array list contains an specific string
May 12, 2022 -

I created an array list out from a constructor, now what I want to do is that the program will only print those set of array list with the item that matches the search key. I tried using arraylist.contains but it won't work. Is there any method I can use to know whether the array contains a specific certain string? Thank you!

๐ŸŒ
Learn
learn.java โ€บ learning โ€บ tutorials โ€บ collections-and-lists โ€บ collections-lists โ€บ arraylist โ€บ contains
Analyzing an ArrayList Object - Learn.java
This method return the index of the first occurrence of Object o if it exists. If the ArrayList does not contain this value, the method returns -1.\
๐ŸŒ
Eduardo Figueiredo
homepages.dcc.ufmg.br โ€บ ~andrehora โ€บ examples โ€บ java.util.ArrayList.contains.11.html
java.util.ArrayList.contains
public class ArrayListDemo { public static void main(String[] args) { // create an empty array list with an initial capacity ArrayList<Integer> arrlist = new ArrayList<Integer>(8); // use add() method to add elements in the list arrlist.add(20); arrlist.add(25); arrlist.add(10); arrlist.add(15); // let us print all the elements available in list for (Integer number : arrlist) { System.out.println("Number = " + number); } // list contains element 10 boolean retval = arrlist.contains(10); if (retval == true) { System.out.println("element 10 is contained in the list"); } else { System.out.println("element 10 is not contained in the list"); } // list does not contain element 30 boolean retval2 = arrlist.contains(30); if (retval2 == true) { System.out.println("element 30 is contained in the list"); } else { System.out.println("element 30 is not contained in the list"); } } }
Top answer
1 of 2
5

Yes. You need to define your NimPlayer class like this (if you want to compare two NimPlayer objects only by their userName)

package com.example.schooltimetable;

import java.util.Objects;

public class NimPlayer {
  private String userName;
  private String familyName;
  private String givenName;
  private int gamesWon;
  private int gamesPlayed;

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getFamilyName() {
    return familyName;
  }

  public void setFamilyName(String familyName) {
    this.familyName = familyName;
  }

  public String getGivenName() {
    return givenName;
  }

  public void setGivenName(String givenName) {
    this.givenName = givenName;
  }

  public int getGamesWon() {
    return gamesWon;
  }

  public void setGamesWon(int gamesWon) {
    this.gamesWon = gamesWon;
  }

  public int getGamesPlayed() {
    return gamesPlayed;
  }

  public void setGamesPlayed(int gamesPlayed) {
    this.gamesPlayed = gamesPlayed;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;
    NimPlayer nimPlayer = (NimPlayer) o;
    return Objects.equals(userName, nimPlayer.userName);
  }

  @Override
  public int hashCode() {
    return Objects.hash(userName);
  }
}

Now to check if the arraylist contains :

public void showCertainPlayerInformation(ArrayList<NimPlayer> playerList, String inputName)
    {
      NimPlayer n = new NimPlayer();
      n.setUserName(inputName);
      if(playerList.contains(n)){
          ....
          return;
      }
      System.out.println("The player does not exist");
     }
2 of 2
2

You can go to method declaration (CTRL + click on method name) and check its implementation. contains() uses equals() to check whether passed object equals any of the elements or not. And equals() declaration can be found in Object class:

ArrayList Class:

public boolean contains(Object var1) {
    return this.indexOf(var1) >= 0;
}

public int indexOf(Object var1) {
    int var2;
    if (var1 == null) {
        for(var2 = 0; var2 < this.size; ++var2) {
            if (this.elementData[var2] == null) {
                return var2;
            }
        }
    } else {
        for(var2 = 0; var2 < this.size; ++var2) {
            if (var1.equals(this.elementData[var2])) {
                return var2;
            }
        }
    }

    return -1;
}

Object Class:

public boolean equals(Object var1) {
    return this == var1;
}
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ arraylist contains method question
r/learnjava on Reddit: ArrayList contains method Question
July 27, 2023 -

Why does this return false? The .contains() method determines whether an ArrayList contains the parameter provided by using the .equals() method to compare the two. Isn't that then overrided by the .equals() method in the Doberman class, and since both Dobermans are named "Chloe", it should return true? Please help me understand.

import java.util.ArrayList;
public class Doberman {
private static int dobieCount = 0;
private String name;
public Doberman(String name) {
    this.name = name;
    dobieCount++;
}
public String reportDobieCount() {
    return name + " says there are " + dobieCount + " dobies.";
}
public boolean equals(Doberman other) {
    return this.name.equals(other.name);
}
    public static void main(String[] args) {
        ArrayList daringDobermans = new ArrayList();
    daringDobermans.add(new Doberman("Chloe"));
    System.out.println(daringDobermans.contains(new Doberman("Chloe")));
    }

}

Top answer
1 of 2
5
The method is not overriden, only overloaded. Overriding means that the new method has exactly the same method signature as the original. Since the original .equals method has the signature boolean equals(Object obj) and your method has the signature boolean equals(Doberman other) they are not the same and therefore the method is overloaded, not overridden, and therefore the original method with Object obj as argument is called. Had you used the optional @Override annotation, the compiler would already have told you that. Read this article on how to properly override ,equals so that it works as expected.
2 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_sort_list.asp
Java Sort a List - List Sorting
Sort a Java list alphabetically or numerically with Collections.sort().
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ find-all-duplicates-in-an-array
Find All Duplicates in an Array - LeetCode
Can you solve this real interview question? Find All Duplicates in an Array - Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears at most twice, return an array of all the integers that appears twice.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ linkedlist-contains-method-in-java
LinkedList contains() Method in Java - GeeksforGeeks
July 11, 2025 - Example: Here, we use the contains() method to check if the element is present in the LinkedList or not. ... // Java programm to Demonstrate the // use of contains() in LinkedList import java.util.LinkedList; public class Geeks { public static ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-arraylistcontainsall-method-in-java
What is the ArrayList.containsAll() method in Java?
The ArrayList.containsAll() method in Java is used to check if the list contains all the elements present in a collection.