Java method to compare whether the values of two lists are equal
- 2020-04-01 04:00:35
- OfStack
This article illustrates a Java method for comparing the values of two lists. Share with you for your reference. The details are as follows:
Suppose there are two queues {1,2,3,4} and {4,3,2,1}. This method compares the values contained in the two queues to be equal
public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) {
if(a.size() != b.size())
return false;
Collections.sort(a);
Collections.sort(b);
for(int i=0;i<a.size();i++){
if(!a.get(i).equals(b.get(i)))
return false;
}
return true;
}
//The test method is as follows:
public static void main(String[] args) {
List<Integer> a = Arrays.asList(1,2,3,4);
List<Integer> b = Arrays.asList(4,3,2,1);
System.out.println(compare(a, b));
}
//Execute the result true
I hope this article has been helpful to your Java programming.