To check if two lists are equal in Java, you can use the equals
method or the Arrays.equals
method.
In this example,
list1
and list2
with initial values.equals
method to check if list1
equals list2
.import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(1, 2, 3);
boolean result = list1.equals(list2);
System.out.println("Are the lists equal? " + result);
}
}
Are the lists equal? true
In this example,
array1
and array2
with initial values.Arrays.equals
method to check if array1
equals array2
.import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array1 = {1, 2, 3};
int[] array2 = {1, 2, 3};
boolean result = Arrays.equals(array1, array2);
System.out.println("Are the arrays equal? " + result);
}
}
Are the arrays equal? true
In this tutorial, we learned How to check if Two Lists are Equal in Java language with well detailed examples.