简体   繁体   中英

How to use the ArrayList.contain() to check the object in java

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM