查找两个不同列表是否包含Java中完全相同的元素的简单方法
如果两个列表包含相同顺序的相同数量的元素,则两个列表相等。
假设我们有以下两个列表-
List<Integer>arrList1 = Arrays.asList(new Integer[] { 10, 20, 30, 45, 55, 70, 90, 100 });
List<Integer>arrList2 = Arrays.asList(new Integer[] {15, 25, 35, 50, 55, 75, 95, 120});现在,让我们找出两个列表是否相等-
arrList1.equals(arrList2);
如果以上两个列表具有相等的元素,则返回TRUE,否则返回FALSE。
示例
import java.util.Arrays;
import java.util.List;
public class Demo {
public static void main(String[] a) {
List<Integer>arrList1 = Arrays.asList(new Integer[] { 10, 20, 30, 45, 55, 70, 90, 100 });
List<Integer>arrList2 = Arrays.asList(new Integer[] {15, 25, 35, 50, 55, 75, 95, 120});
List<Integer>arrList3 = Arrays.asList(new Integer[] { 10, 20, 30, 45, 55, 70, 90, 100});
System.out.println("Are List 1 and List2 equal? "+arrList1.equals(arrList2));
System.out.println("Are List 2 and List3 equal? "+arrList2.equals(arrList2));
System.out.println("Are List 1 and List3 equal? "+arrList1.equals(arrList3));
}
}输出结果
Are List 1 and List2 equal? false Are List 2 and List3 equal? true Are List 1 and List3 equal? true