How to compare two ArrayList in Java?

Learn to compare two ArrayLists in Java through simple examples. We will first test whether two ArrayLists are equal. If the two lists are not equal, we will find the differences between the lists.

The difference in the list is equal to another third list that contains additional or missing elements.

In addition, learn to find common elements between two ArrayLists.

See the original post: https://www.java8net.com/2020/03/compare-two-arraylist-in-java.html

Compare two ArrayLists for equality

With the equals () method, we can compare two ArrayLists in Java and check if they are the same. However, this is a condition, the order of elements of the two ArrayList must be the same. Otherwise, it will not work properly. To be sure, we can sort the ArrayList (sorting can be ascending or descending), and then apply the equals () method for comparison.

Read Article to learn how to sort ArrayList in Java: https://www.java8net.com/2020/02/how-to-sort-arraylist-in-java.html

Import java.util.ArrayList;

公共类ArrayListExample { 公共静态void main(String [] args){ ArrayList list1 = new ArrayList(); list1.add(“ A”); list1.add(“ B”); list1.add(“ C”); list1.add(“ D”); ArrayList list2 = new ArrayList(); list2.add(“ A”); list2.add(“ B”); list2.add(“ C”); list2.add(“ D”); System.out.println(list2); System.out.println(list1.equals(list2)); } }

If you want to learn in detail: https://www.java8net.com/2020/03/compare-two-arraylist-in-java.html

Further Reading:
https://www.java8net.com/2020/03/java-arraylist-common-program-examples.html
https://www.java8net.com/2020/03/java-sublist-from-arraylist.html
https://www.java8net.com/2020/03/how-to-remove-duplicates-from-arraylist-in-java.html
https://www.java8net.com/2020/03/iterate-arraylist-in-java.html
https://www.java8net.com/2020/03/arraylist-to-array-in-java.html

from: https://dev.to//aryan19694946/how-to-compare-two-arraylist-in-java-499b

Published 0 original articles · liked 0 · visits 411

Guess you like

Origin blog.csdn.net/cunxiedian8614/article/details/105690061