I encountered the same problem, and write a comparison function:
/**
* Compare two sequences of lines without considering order.
* <p>
* Input parameter will not be modified.
*/
public static <T> boolean isEqualWithoutOrder(final T[] lines1, final T[] lines2) {
if (lines1 == null && lines2 == null) return true;
if (lines1 == null) return false;
if (lines2 == null) return false;
if (lines1.length != lines2.length) return false;
final int length = lines1.length;
int equalCnt = 0;
final boolean[] mask = new boolean[length];
Arrays.fill(mask, true);
for (int i = 0; i < lines2.length; i++) {
final T line2 = lines2[i];
for (int j = 0; j < lines1.length; j++) {
final T line1 = lines1[j];
if (mask[j] && Objects.equal(line1, line2)) {
equalCnt++;
mask[j] = false;
//if two equal lines is found, more subsequent equal lines are speculated
while (j + 1 < length && i + 1 < length &&
Objects.equal(lines1[j + 1], lines2[i + 1])) {
equalCnt++;
mask[j + 1] = false;
j++;
i++;
}
break;
}
}
if (equalCnt < i) return false;
}
return equalCnt == length;
}
Common collections may be slow, speed comparison:
//lines1: Seq[String], lines2: Seq[String] of 100k lines of equal Random String but without ordering.
FastUtils.isEqualWithoutOrder(lines1.toArray, lines2.toArray) //97 ms
lines1.sorted == lines2.sorted //836 ms
Time measured in hot sbt environment.
(Disclaimer: I only did some basic test against this function)