2

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 there is a way to do such a check using contains method.

I've referred to this post: How does a ArrayList's contains() method evaluate objects?

But I am new to java and don't really the meaning of the method. Does it mean to override the equals() method to check the object? Any help is highly appreciated.

Here is my method trying to check if the user exists in the playerList.

public void showCertainPlayerInformation(ArrayList<NimPlayer> playerList, String inputName)
    {
        System.out.println("The request to show the name: "+ inputName);
        
        //trying to use the contains method, but it seems impossible
        //if (playerList.contains(NimPlayer.getUserName().equals(inputName)))

        for (NimPlayer player : playerList) 
        {
            String userCheck = player.getUserName();
            String familyName = player.getFamilyName();
            String givenName= player.getGivenName();
            int gamesWon = player.getGamesWon();
            int gamesPlayed = player.getGamesPlayed();
            
            if (userCheck.equals(inputName)) 
            {
                System.out.println(inputName + "," + givenName + "," 
                + familyName + "," + gamesPlayed + " games," + gamesWon + " wins");
                return;
            } 
                 
        }

        System.out.println("The player does not exist");
    }
Woden
  • 1,054
  • 2
  • 13
  • 26
  • 2
    Yes, contains() internally use equals method to check two object are equals or not when compare your given object with existing list object. So if your override equals then your method will be used for this. – Eklavya Jul 03 '20 at 15:21
  • And if you need only to check in this function, is list contains any player with username then you can use stream API's `anyMatch()` (as you only compare with username). – Eklavya Jul 03 '20 at 15:38

2 Answers2

4

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");
     }
Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37
  • 2
    I suggest you add a link to Question(s) about always overriding `hashCode` when overriding `equals`. https://stackoverflow.com/q/17027777/642706 – Basil Bourque Jul 03 '20 at 15:27
  • I think OP question is about _Does it mean to override the equals() method to check the object?_ . – Eklavya Jul 03 '20 at 15:28
  • Thank you @BasilBourque . Now, I understand why they're overridden together. – Woden Jul 03 '20 at 15:47
  • I have a further question regarding this. What if the `NimPlayer` class is an abstract class? – Woden Jul 03 '20 at 15:56
  • 1
    @Woden Doesnt matter. Any concrete subclass extending that abstract class will have these overriden equals and hashcode and hence you can use list.contains(..) – Abhinaba Chakraborty Jul 03 '20 at 16:28
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;
}
Sercan
  • 2,081
  • 2
  • 10
  • 23
  • Thank you for your answer and suggestion. – Woden Jul 03 '20 at 15:42
  • 1
    You are welcome. Please consider upvote or/and accept an answer when it is useful or solves your problem, instead of thanking in comment. It is SOF way to say thank you :) – Sercan Jul 03 '20 at 15:47