Dear all ,

This mail , thou its slightly off-topic , describes an ecstatic experience
while programming in java. We group of 2( now thats not a group!!...)  are
developing a prototype of a buysite/marketsite on J2EE architecture.  We
encountered a strange problem comparing two objects. We have an
Arraylist(AL1) of objects of say Class A. We have another ArrayList (AL2)of
objects of the same Class A. In the second there are objects which may
contain "equal" objects( all the attributes same of that in AL1) and
"unequal" objects. Our Job was to filter those objects that are not present
in AL2. ArrayList has an alluring method to handle this ...the  contains()
method. We proceeded implementation of this small job of ours using a
simple logic of comparing objects in AL1 with that of AL2. Strangely enough
the contains() method returned false even when it compared "equal" objects
in  list AL1. Now this was really intriguing.

Now I was wondering how this was happening. Does the comparison happen on
the basis of reference held by each object?? ...that was silly ....then
there is no point of having a contains or method or an equals method
......so where were we faltering .....we knew that we were making a mistake
on the core fundamental java side....so i decided to write a small program
.....as shown below....

import java.util.*;
public class ListContains{
                  public static void main(String arg[]){

                         ArrayList balls =new ArrayList();
                         ArrayList testBalls = new ArrayList();
                         Ball b1 = new Ball("Red",2);
                         Ball b2 = new Ball("blue",1);
                         Ball b3 = new Ball("green",3);
                         Ball b4 = new Ball("Red",2);

                         balls.add(b1);
                         balls.add( b2);

                         System.out.println(balls.contains(b4));

                    }
                    }
                    class Ball{
                              String color ;
                              int size;
                              public Ball(String color,int size){
                                   this.color=color;
                                   this.size = size;
                              }
                    }

        }

here my o/p was "false"....surprisingly again as i told before...thou ball b1 and b4 
are the same why isn't my contains() method returning true
.....at this point i decided to consult an able java guy
at the office ...he explained with a smile  and just said one statement .."you have be 
to implementing the "equals()" method in your class Ball
"...that answered all my doubts regarding the contains method

because contains() essentially  works on the basis of the equals method ...and so i 
just had to implement the equals() method and the hashcode()
method in my class Ball
With a small modification in my class Ball as shown below i could achieve my desired 
result.. and so did I with my Class A ......this experience was
so ecstatic ...the thinkers of Java have certain things so logical that

you dont really have to try hard to figure out the actual problem..hats off to the 
guys at Sun ...i hope this mail helps some to solve , learn and
appreciate Java.

This one should also enlighten the essentiality of equals() and hashcode() methods in 
Primary Key Class !!......



             class Ball{


                     String color ;
                     int size;
                     public Ball(String color,int size){
                          this.color=color;
                          this.size = size;


                     }

                     public int hashCode() {

                               return 1;//return something

                     }
                     public boolean equals(Object that) {

                     if (!(that instanceof Ball))
                          return false;

                     Ball tmp = (Ball)that;
                     return (this.color.equals(tmp.color));

                     }

             }




     regards to all ,
     Thanks for reading such a  lengthy mail !!!!
     Vinodh
     ----------------------------------------------------------------
     The information transmitted is intended only for the person or entity to which it 
is addressed and may contain confidential and/or privileged
     material.  Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon, this information by persons or
     entities other than the intended recipient is prohibited.   If you received this 
in error, please contact the sender and delete the material from
      any computer.

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to