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");
     }
Answer from Abhinaba Chakraborty on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-contains-java
Arraylist.contains() Method in Java - GeeksforGeeks
December 10, 2024 - Example 2: Here, we are using the contains() method to check if the ArrayList of Integers contains a specific number. ... // Java program to demonstrate the use of contains() // method with ArrayList of Integers import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Create an ArrayList of Integers ArrayList<Integer> arr = new ArrayList<>(); arr.add(10); arr.add(20); arr.add(30); // Check if 20 exists in the ArrayList System.out.println(arr.contains(20)); // Check if 40 exists in the ArrayList System.out.println(arr.contains(40)); } }
🌐
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. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report ...
Discussions

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
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
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 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
🌐
Codecademy
codecademy.com › docs › java › arraylist › .contains()
Java | ArrayList | .contains() | Codecademy
February 22, 2024 - It is used to check if the element is present in the specified ArrayList or not. The function returns a boolean value of true if the element is present and false if not. ... Looking for an introduction to the theory behind programming?
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 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.
🌐
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.
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;
}
🌐
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"); } } }
Find elsewhere
🌐
Java Development Journal
javadevjournal.com › home › arraylist contains() method
ArrayList contains() Method | Java Development Journal
July 20, 2021 - 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 ...
🌐
Programiz
programiz.com › java-programming › library › arraylist › contains
Java ArrayList contains()
The contains() method checks if the specified element is present in the arraylist. import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<String> languages = new ArrayList<>(); languages.add("Java"); languages.add("Python"); ...
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › contains
Java ArrayList contains() - Check If Contains Element | Vultr Docs
November 27, 2024 - ArrayList<Integer> numbers = new ... is taken. The contains() method in Java's ArrayList provides a simple yet powerful way to check for the presence of an object....
🌐
W3Resource
w3resource.com › java-tutorial › arraylist › arraylist_contains.php
Java ArrayList contains Method - w3resource
August 19, 2022 - Java ArrayList.contains() Method with example: The contains() method is used to determines whether an element exists in a ArrayList object. Returns true if this list contains the specified element.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-contains-method-example
Java ArrayList contains() Method example
September 11, 2022 - Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java · ArrayList contains() method is used for checking the specified element existence in the given list. public boolean contains(Object element) It returns true if the specified element is found in the list else it gives false.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › check if element exists in an arraylist in java
Check if Element Exists in an ArrayList in Java - HowToDoInJava
February 20, 2023 - 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.
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › containsAll
Java ArrayList containsAll() - Check All Elements Presence | Vultr Docs
December 3, 2024 - The containsAll() method in Java's ArrayList class is designed to determine if one list contains all elements of another list.
🌐
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!

🌐
Codekru
codekru.com › home › arraylist contains() and containsall() method in java
ArrayList contains() and ContainsAll() method in Java - Codekru
August 12, 2022 - java · This post will discuss the contains() and containsAll() methods implemented in the ArrayList class. They are used to check whether an ArrayList contains a specified element/Collection or not. public boolean contains(Object o) public boolean containsAll(Collection c) Let’s discuss ...
🌐
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.
🌐
Programiz
programiz.com › java-programming › library › arraylist › containsall
Java ArrayList containsAll()
In the above example, we have created two arraylists named languages1 and languages2. Notice the expression, ... Here, the containsAll() method checks if languages1 contains all elements of languages2. Hence, the method returns true.