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

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??
0
cathalmchale
Asked:
cathalmchale
  • 7
  • 6
  • 2
  • +1
3 Solutions
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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