views:

90

answers:

2

Hello,

i try to compare two lists with each other:

ListA (a1,a2,a3,...)
ListB (b1,b2,b3,...)

I want that a1 is compared to b1, a2 to b2, a3 to b3, ....

But i have to use another method and cannot use .equals!

I have written my own hamcrest matcher. But i have to use a for loop to iterate over the elements. is there a better solution?

for(int i = 0;i<expected.size();i++){
   assertThat(item.get(i),equalsModel(expected.get(0)));
}
A: 

You can simply use the equals method in List and implement the equals method for the Objects in your List the way you want. Or you can take a look at the org.apache.commons.collections.CollectionUtils API. It provides a lot of methods to compare collections.

Jeroen Rosenberg
I think he means the `equals()` method on the objects in the list cannot be used for this purpose, he needs to use custom logic to check equality, therefore `List.equals()` won't work.
skaffman
That is the problem, yes ...
simonh
+1  A: 

How about using iterators instead?

for(
    Iterator<String> it1 = list1.iterator(), it2 = list2.iterator();
    it1.hasNext() && it2.hasNext();
){
    assertThat(it1.next(),equalsModel(it2.next()));
}
seanizer
hm, goes into that direction. i would like to have something like this:assertThat(list1,equalsToList(list2).using(selkowsTreeDistance()));
simonh