查找两个不同列表是否包含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
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短