Hi Friends,
I have a custom class Person and I would like to store only unique Person objects into a hashmap. But I am confused on how to compare objects using the hashmap's containKey method.
So,I am trying something like this:
HashMap<Person,Integer> persons = new HashMap<Person,Integer>();
Person p = new Person("tester"); //person constructor with name
//add person object only if it does not exists
if(persons.containsKey(p)){ // this is what i am confused with
//do something
}
else{
persons.add(p,1);
}
But I am connfused how to compare objects of a particular class??? Do I need to add "equals" or some other method to the Person class or is there any other way???
Any help would be appreciated
Thanks