-1

i'm new in java collections so i tried to codes using Map. i set my collection like this

    Map<Integer, Person> people = new HashMap<>();                                     
    people.put(1, new Person("Arnold", "Maluya", 25));
    people.put(2, new Person("Mison", "Drey", 3));
    people.put(3, new Person("James", "Valura", 54));
    people.put(4, new Person("Mikee", "Sandre", 24));

so my goal is i want to check if people contains object like "new Person("Arnold", "Maluya", 25)" so what i did is this

 boolean test = people.containsValue(new Person("Arnold", "Maluya", 25));

 System.out.println(test);

which is i getting "false" result. so am i getting it right so if sumthing is wrong what did i miss?

cute_programmer
  • 332
  • 3
  • 13

3 Answers3

3

Implement an equals, example:

public class Person {

private String name;

private String lastName;

private String age;

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

    Person person = (Person) o;

    if (name != null ? !name.equals(person.name) : person.name != null) return false;
    if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) return false;
    return age != null ? age.equals(person.age) : person.age == null;
}

@Override
public int hashCode() {
    int result = name != null ? name.hashCode() : 0;
    result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
    result = 31 * result + (age != null ? age.hashCode() : 0);
    return result;
}
}
Merch0
  • 594
  • 5
  • 17
1

The methods hashCode() and equals() play a distinct role in the objects you insert into Java collections.

equals() is used in most collections to determine if a collection contains a given element.

When inserting an object into a hastable you use a key. The hash code of this key is calculated, and used to determine where to store the object internally. When you need to lookup an object in a hashtable you also use a key. The hash code of this key is calculated and used to determine where to search for the object.

When you use your custom java objects in collections, its always advisable to override hashCode() & equals() methods, to avoid weird behaviors.

Jaydeep Rajput
  • 3,605
  • 17
  • 35
0

The behavior is correct as you are not overriding the equals method in Person class. Map will consult with equals method of the object stored in it to identify whether the query is matching with stored values. You must override the equals method in your object and implement logic appropriately to determine whether object passed as an argument is matching or not.

Note: Below code doesn't check for null values and hence may throw an exception. You need to put additional conditions to avoid null pointer exceptions.

@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Person)) {
        return false;
    }

    Person other = (Person) obj;

    if ((other.firstName.equals(this.firstName)) && (other.lastName.equals(this.lastName))
            && (other.age == this.age)) {
        return true;
    }

    return false;
}
CuriousMind
  • 3,143
  • 3
  • 29
  • 54