Search code examples
javacollectionshamcrest

Using hamcrest for comparing each item in two separate lists with own matcher


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

Solution

  • How about using iterators instead?

    for(
        Iterator<String> it1 = list1.iterator(), it2 = list2.iterator();
        it1.hasNext() && it2.hasNext();
    ){
        assertThat(it1.next(),equalsModel(it2.next()));
    }