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();

l1.add(objA1);
l1.add(objB1);

l2.add(objA2);
l2.add(objB2);

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??
cathalmchaleAsked:
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.

CEHJCommented:
>>So l1 should equal l2  !!

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

0
CEHJCommented:
The method is equals, btw. By what criteria are you trying to say that they're equal?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

zzynxSoftware engineerCommented:
about equals() on a List:

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

Your issues matter to us.

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

Start your 7-day free trial
CEHJCommented:
Sorry - i'm wrong - they *are* equal ;-)
0
zzynxSoftware engineerCommented:
>>l1.add(objA1);
>>l1.add(objB1);

>>l2.add(objA2);
>>l2.add(objB2);

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

0
petmagdyCommented:
this is true if u implemented equals() method for ObjectTypeB, ObjectTypeA correctly
0
zzynxSoftware 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
cathalmchaleAuthor 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
CEHJCommented:
If you can't change the return type of getList to List<Schedule> then you'll have to ignore the warning
0
petmagdyCommented:
what is the type of this and what type this.getList(); returns?
0
zzynxSoftware 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
zzynxSoftware 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
CEHJCommented:
8-)
0
zzynxSoftware 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.