public static < T , U > String compareIterators ( Iterator < U > iterator , Iterator < T > expectedIterator , Function < U , T > mapping ) { String errorMessage = "" ; while ( iterator . hasNext ( ) ) { U next = iterator . next ( ) ; T nextMapped = mapping . apply ( next ) ; T nextExpected = expectedIterator . next ( ) ; if ( ! nextMapped . equals ( nextExpected ) ) { errorMessage += "Expecting " + nextExpected + " but got " + nextMapped ; } } return errorMessage ; }
Walks down two iterators comparing them element by element using the equals method .