I repeat the two collections and check if both collections contain the same elements . I can not use Java 8.
Both collections contain elements that are comparable , and the content is defined as equal if all elements return a x.compareTo(y) with 0.
Two values ββare defined as different if one of them is zero, but not the other . I want to find an elegant way to compare by invalidity and prevent a zero check of the final compareTo() .
My current implementation:
public static <T extends Comparable<T>> boolean isSame(@Nullable Collection<T> a, @Nullable Collection<T> b) { if (a == null || b == null) { return (a == null && b == null); } if (a.size() != b.size()) { return false; } Iterator<T> aIt = a.iterator(); Iterator<T> bIt = b.iterator(); while (aIt.hasNext()) { T aValue = aIt.next(); T bValue = bIt.next(); if (aValue == null || bValue == null) { if (aValue == null ^ bValue == null) { return false; }
I want to continue the while loop if both values ββare zero, as this is defined as equal.
But I'm struggling with this part:
if (aValue == null || bValue == null) { if (aValue == null ^ bValue == null) { return false; } }
Question:
Is there a more elegant and readable way to compare for invalidity, do an additional comparison if both values ββare non-zero, return false if only one of them is zero, and continue the cycle if both values ββare zero?
source share