查找两个不同列表是否包含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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短