# equalsTo

Hi,

Suppose I create 2 lists

List l1 = new ArrayList();
List l2 = new ArrayList();

ObjectTypeA objA1 = new ObjectTypeA();
ObjectTypeA objA2 = new ObjectTypeA();

ObjectTypeB objB1 = new ObjectTypeB();
ObjectTypeB objB2 = new ObjectTypeB();

So l1 should equal l2  !!
so I can call   l1.equalsTo( l2 );
Is that correct.
Then I presume all I have to do to get a positive result is over-ride the equalsTo() method in both classes   ObjectTypeA and ObjectTypeB??
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
>>So l1 should equal l2  !!

No, as they contain different objects
0
Software engineerCommented:
>> so I can call   l1.equalsTo( l2 );
Make that l1.equals(l2);

0
Commented:
The method is equals, btw. By what criteria are you trying to say that they're equal?
0
Software engineerCommented:

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Sorry - i'm wrong - they *are* equal ;-)
0
Software engineerCommented:

>>So l1 should equal l2  !!
only if objA1.equals(objA2) and objB1.equals(objB2)
0
Commented:
*If* ObjectTypeB and ObjectTypeA implement equals themselves properly - otherwise it won't work

0
Commented:
this is true if u implemented equals() method for ObjectTypeB, ObjectTypeA correctly
0
Software engineerCommented:
>> *If* ObjectTypeB and ObjectTypeA implement equals themselves properly
>> this is true if u implemented equals() method for ObjectTypeB, ObjectTypeA correctly
"properly" / "correctly" =

in such a way that
objA1.equals(objA2) and objB1.equals(objB2)
0
Author Commented:
A different question, but wrt the same problem!

if I do
List<Schedule> l1 = this.getList();

the call this.getList();  gets a List object which is not generic.
Is this code OK as long as I catch a classCastExcept ?? or do i have to cast the items of the list individually?
0
Commented:
If you can't change the return type of getList to List<Schedule> then you'll have to ignore the warning
0
Commented:
what is the type of this and what type this.getList(); returns?
0
Software engineerCommented:
>> call this.getList(); gets a List object which is not generic
???
As long as it returns a List containing Schedule objects it's OK, I guess.
0
Software engineerCommented:
>> or do i have to cast the items of the list individually?
You could do that, but I think that's unnecessary workload
0
Commented:
8-)
0
Software engineerCommented:
Thanks
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.