简体   繁体   中英

Compare a string value with arraylist<object>

I have a class of persons.

Public class Person{

private String name = "";

    public Person(String name) {
        
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

Now I have another class and inside the class I declared an arraylist

public class AddPerson {
public ArrayList<Person> pers = new ArrayList<Person>();

//here we add some persons to the arraylist
pers.add(new Person("Simon"));
pers.add(new Person("Oscar"));
pers.add(new Person("Alfred"));

String name = "Simon";
pers.contains(name); //return false
pers.equals(name); //return false 

//I also want to be able to return the value and index of the name in arraylist if it exsists.
if(pers.contains(name)) //return index and value
}

When I try to check if two strings are equal or if a string is already in my list I get both false. I did some research and I saw that I need to override my equals method (and mabey hash and contains method as well). I do not know how to do it and I could not find a good reference how to do it. Please help me to achive that.

That is because you are trying to compare a string ( "simon" ) and an object Person built from a string ( Person("Simon") ). These are two different objects, and it is thus normal to have false as a result.

Whether you want to compare two Person (this would make sense) or you are actually trying to compare a string and a person are it is most certainly a bad idea (you can't actually compare apples and oranges, well this is the same here).

What you might want to do is whether

  1. to override Person 's equals method to, for example, return true if they have the same name . => comparing Person s
  2. to iterate on your pers array and fetch it's name property via getName() to compare it with you name string => comparing String s

1 =>

@Override
public boolean equals(final Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    final Person person = (Person)o;
    return name.equals(person.name);
}

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

2 =>

for (final Person person : pers) {
    if(name.equals(person.getName())){
        // ...
    }
}

Here the problem is you are comparing a Person object with String object using the contains method.So actually in this case there is no point of implementing equals() method and hashCode() methods or using the contains() method to solve this .Only you have to do is check if person's attribute name is equal to your comparing String called name.

Sample solution:

String name="Simon";

Person result  = null;//To store the person object if a person name Simon found in the list
int personIndex = -1; //To store the index of founded person
int indexCount =-1;//To maintain the current index value of arrayList

for(Person tempPerson:pers){//Iterating the list to find a person name Simon
   indexCount++;
   if(pers.getName().equals(name)){
         personIndex = indexCount;
         result = tempPerson;
    }
}

Now you can use the values in personIndex and indexCount variables for future calculations.You can use a condition like if(personIndex >= 0) to check if really a person found with the comparing name after the iteration and then you can get the result variable value.

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