#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Equals method is not being called


    Hello. My equals method is not being called and i cannot see why any help? thanks in advance!
    Code:
    	@Override 
    	public boolean equals(Object o){
    		Assessment a=(Assessment) o;
    		if(this.getCandidate().equals(a.getCandidate())){
    			if(this.getAnswers().equals(a.getAnswers())){
    				if(this.getDate().equals(a.getDate())){
    					System.out.println("I am here");
    					return true;
    				}
    			}
    		}
    		return false;
    	}
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,644
    Rep Power
    1945
    How do you use the equals function? Please post the relevant code.
    How do you know it is not called? do you get an error? unexpected behaviour?
    If you add a println() at the first line in the function, will this be printed out?
    Have you checked that the variables has the correct/expected values?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by MrFujin
    How do you use the equals function? Please post the relevant code.
    How do you know it is not called? do you get an error? unexpected behaviour?
    If you add a println() at the first line in the function, will this be printed out?
    Have you checked that the variables has the correct/expected values?
    Code:
    public boolean eqArL(ArrayList<Assessment> arr, Assessment a){
    		boolean eq=false;
    		for(int i=0; i<arr.size(); i++){
    			if(arr.get(i).equals(a)){
    				System.out.println("spotted")!
    				eq=true;
    				break;
    			}
    		}
    		return eq;
    	}
    public static void main(String[] args){
    	AssessmentP a1=new AssessmentP();
    	ArrayList<Assessment> arr=a1.getArray();
    //some assessment the same c, map and date was added before so according to my equals it should not add it again but it does 
    Assessment c= new Assessment(c, this.map, this.date);
    	System.out.println(a1.eqArL(c));
    }
    in my code sorry cannot add it cause it is really long It always adds the same objects to the arraylist to prevent this I decided to override equals method but it is not called I did what you said "add a println() at the first line in the function" but it is still not called
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,644
    Rep Power
    1945
    When you call a1.eqArL(c), you are only adding an Assessment object as parameter, it should also have an ArrayList as parameter.

    Did you write eqArL or was it given to you?
    What classes do you have? Assessment? AssessmentP? Does AssessmentP extend Assessment?

    Remember, the more specific you are about the issue, the easier will it be for us to help you.

    It always adds the same objects to the arraylist
    The only place I see you add something to the arraylist is a1.getArray();
    Does getArray() works as expected?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    33
    Remember that the default Object .equals() compares the object references, not what they contain. Also, using == compares the memory references directly (thanks for the heads up NormR).

    For example. Let's assume you have a class called StringEqualityObject. This object is completely empty - no methods or parameters. It has a single constructor which takes no parameters. Therefore, to a human, if you create one then another, those two are logically identical (they are both empty).

    However Java sees it differently because each object resides in it's own space in memory - therefore to Java, they are NOT equal.

    See for yourself....

    Code:
    public class StringEqualityObject
    {
        public StringEqualityObject()
        {
        }
    }
    Code:
    public class StringEquality
    {
        public StringEquality()
        {
            StringEqualityObject first = new StringEqualityObject();
            StringEqualityObject second = new StringEqualityObject();
     
            if (first.equals(second)) System.out.println("Objects are equal");
            else System.out.println("Objects are not equal");
        }
    
    }
    Last edited by doa24uk; April 7th, 2013 at 11:44 AM.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    .equals() compares the object references, not what they contain.
    Did you mean the == operator?
    equals() can look at the contents of an object depending on how it is written
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    33
    Sorry, I meant the default object equals.

    I've edited my post. Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo