List of Generics equality
Case
In leetcode no. 39 Combination Sum gives
Given an array of distinct integers
candidates
and a target integertarget
, return a list of all unique combinations ofcandidates
where the chosen numbers sum to target. You may return the combinations in any order.The same number may be chosen from
candidates
an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.It is guaranteed that the number of unique combinations that sum up to
target
is less than150
combinations for the given input.Example
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be use mutiple times. 7 is a candidate, and 7 = 7. These are the only two combinations.
List Equality
Consider the following case:
List<Integer> a = Arrays.asList(1,2,3,4);
List<Integer> b = Arrays.asList(4,3,2,1);
List<Integer> c = Arrays.asList(4,4,3,2,1);
If we apply containsAll
methods to test List equality in any order, it might work. But it might not test size of List, so it might go wrong when there is repeated items.
System.out.println(b.containsAll(a)) // true
System.out.println(c.containsAll(a)) // true
How about applying equals
methods after sorting the List?
// apply sort to all to-be-check items.
Collections.sort(a);
Collections.sort(b);
Collections.sort(c);
System.out.println(a.equals(b)); // true
System.out.println(a.equals(c)); // false
Luckily, List can easily to test elements equality rathan than strict equality of the same object.
So, We can see if we want to test List Equality in any order, we can just simply sort List, and apply a.equals(b)
.
List of List Equality
But in case leetcode no. 39 Combination Sum, list of list of Integer in any order is considered acceptable answer. How do we test list of list of Integer equality?
class Solution{
public List<List<Integer>> combinationSum(int[] candidates, int target){..}
@Test
public void test(){
Solution sol = new Solution();
expected = List.of(List.of(2,2,3), List.of(7)) // [[2,2,3],[7]]
actual = sol.combinationSum(new int[]{2,3,6,7}
Collections.sort(expected); // fail
Collections.sort(actual); // fail
assertEquals(expected, acutal)
}
}
The code will fail, and the description shows below.
The method sort(List
) in the type Collections is not applicable for the arguments (List<List >)Java(67108979)
Thus, we have to turns List of Integers in the List into some other type like String, by using toString()
. So, we can do like this:
public boolean equalsAnyOrder(List<List<Integer>> expected, List<List<Integer>> actual){
// First we have to check size of list equaltiy
if (expected == null && actual == null) return false;
if ((expected == null && actual != null) || (expected != null && actual == null) || expected.size() != actual.size()) return false;
// Create List of string to turn List of Integer into strings.
List<String> c = new ArrayList<>();
List<String> d = new ArrayList<>();
for (int i = 0; i < expected.size(); i++){
c.add(expected.get(i).toString());
d.add(actual.get(i).toString());
}
// Sort List of strings
Collections.sort(c);
Collections.sort(d);
// Apply simple list equaltiy (We have known List.equals() just test equality of elements in order.)
return c.equals(d);
}