• Status: Solved
• Priority: Medium
• Security: Public
• Views: 791

# 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??
0
cathalmchale
• 7
• 6
• 2
• +1
3 Solutions

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

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

## Featured Post

• 7
• 6
• 2
• +1
Tackle projects and never again get stuck behind a technical roadblock.