0

I have this class:

public class Phone{ 
    private String number;
    //get and set of class
}

Then i have another class that uses the "Phone"

public class Antena{
   private String name;
   private ArrayList<Phone> phones;
}

Now before add an element i would like only add if the element don't exist in the array. I'm new in Java and after some research i have found that i need to override the equals method. So can anyone give me some hints please?

This is how i use the equals method:

if(!phones.equals(phone))
   phones.add(phone);
user2295277
  • 139
  • 1
  • 2
  • 9

2 Answers2

1

The comments now have enough information to actually answer the question.

Part 1

Implement equals:

public boolean equals(Object obj) { 
  if (obj == null) return false;
  if (obj.getClass() != this.getClass()) return false;
  Phone t = (Phone ) obj; 
  return t.number.equals(this.number);
}

Part 2

Implement hash code

public int hashCode() {
  return number.hashCode();
}

Part 3

Use a set instead of an ArrayList to require uniqueness..

private HashSet<Phone> phones;
Jeanne Boyarsky
  • 11,880
  • 2
  • 44
  • 58
  • `hashCode()` returns an `int`, not a `long`. – user2336315 Dec 08 '13 at 15:02
  • And equals() should not throw a ClassCastException if the argument is not a Phone. – JB Nizet Dec 08 '13 at 15:03
  • Actually you should check: if (obj instanceof Phone) or you will receive an exception if you for instance have a String as Object – Simon Dirmeier Dec 08 '13 at 15:03
  • Oops. Fixed. As you can see, I usually use my IDE to generate this method! – Jeanne Boyarsky Dec 08 '13 at 15:03
  • Re equals - I was trying to show it the way the original poster did. Split them into two examples to make this clearer. – Jeanne Boyarsky Dec 08 '13 at 15:06
  • Yes, but it's not just that one is better than the other. It's that one is correct and the other is not. – JB Nizet Dec 08 '13 at 15:08
  • I don't disagree. I guess I was trying to highlight == vs equals poorly. I'll remove it since it isn't making the point I was trying to make. – Jeanne Boyarsky Dec 08 '13 at 15:12
  • I can't use an Set because the Teacher want us to use an ArrayList. I have tried your code and it still adds the phone if already exists another with the same number! – user2295277 Dec 08 '13 at 15:13
  • 1
    @user2295277: You don't have to do anything with a Set to prevent duplicates. With a List, you have to do it by yourself: `if (!list.contains(phone)) { list.add(phone);}`. You should learn to read the javadoc: it shows all the available methods and explain what they do and how they work. – JB Nizet Dec 08 '13 at 15:24
0

You override equals method to do equality test of the class objects. For example you have two Phone objects p1 and p2.

Phone p1 = new Phone();
Phone p2 = new Phone();
p1.setNumber(10);
p2.setNumber(10);
\\Ideally these two objects are equal but when you try to do equality test , it will return false.
System.out.println(p1==p2);  // outputs false because p1 and p2 are two different references 

holding different memory addresses. Also ,

System.out.println(p1.equals(p2));  // prints false when not overrided 

When you override equals method you can add a criteria to logically test these two objects.

public boolean equals(Object obj)
  {

    if (!(obj instanceof Phone)) 
      return false;
    return this.getNumber() == ((Phone)obj).getNumber();
  }

Now if you call equals method on p1 and p2 objects , it will return true. Equals method test is useful when you want to equate two class objects on basis of instance variables and not reference addresses.

Nishant Lakhara
  • 1,943
  • 3
  • 21
  • 42