public static <T> boolean sameElementsWithoutOrder(Iterator<T> iterator1,
                                                       Iterator<T> iterator2) {
        // Check both the iterators have the same set of elements irrespective of order and duplicates.
        Set<T> allSegmentsSet = new HashSet<>();
        iterator1.forEachRemaining(allSegmentsSet::add);
        Set<T> expectedSegmentsSet = new HashSet<>();
        iterator2.forEachRemaining(expectedSegmentsSet::add);

        return allSegmentsSet.equals(expectedSegmentsSet);
    }
Returns true if both the iterators have same set of elements irrespective of order and duplicates.

@param iterator1 first iterator.
@param iterator2 second iterator.
@param <T>       type of element in the iterators.
clients/src/test/java/org/apache/kafka/test/TestUtils.java