在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP