You are right in saying that if they are arrays you can't use Arrays.equals(), if they are List<>s then you cannot just use equals. In both cases they also check order.

So yes your main option without writing your own comparison algorithm is to use Collections.sort() on both lists.

If you don't need to check for duplicates you could drop both lists into a HashSet using addAll and then compare the two HashSet or just use List.containsAll. The bad news though is that these would both have the same limitation, if you need to compare lists that might have repeated elements then it may give incorrect results. i.e. "bob", "bob", "fred" would compare as equal to "bob", "fred", "fred".

Answer from Tim B on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 58358599 › how-to-compare-two-java-collections-of-lists-of-strings-with-least-code
How to compare two java collections of lists of strings with least code? - Stack Overflow
If you want to preserve their current order, copy each list into new List objects; this is fast, as the element objects are not copied, only the references to those element objects are copied. I recommend the second option, not the first. Basically, I know how to solve this, I just wondering how this can be done with least code. Just do it. Write the Person class and compareTo & equals & hashCode methods. No issue of "least code", here just comparing fields. Using the new functional features of Java with lambda syntax can shorten the code.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › compare two lists in java: equality, differences, common items
Compare Two Lists in Java: Equality, Differences, Common Items
1 week ago - The following example compares the stock list of a warehouse with the list of products shown in a web shop. List<String> warehouse = List.of("apple", "banana", "cherry"); List<String> shop = List.of("cherry", "banana", "date"); List<String> reordered = List.of("banana", "cherry", "apple"); boolean equal = warehouse.equals(shop); // false boolean sameIgnoringOrder = warehouse.stream().sorted().toList() .equals(reordered.stream().sorted().toList()); // true List<String> onlyInWarehouse = new ArrayList<>(warehouse); onlyInWarehouse.removeAll(shop); // [apple] List<String> missingInWarehouse = new ArrayList<>(shop); missingInWarehouse.removeAll(warehouse); // [date] List<String> common = new ArrayList<>(warehouse); common.retainAll(shop); // [banana, cherry]
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparing-two-arraylist-in-java
Comparing two ArrayList In Java - GeeksforGeeks
January 23, 2026 - Prints true because both lists have the same size and elements in the same order. ... Example: This program demonstrates how to compare two ArrayList objects in Java using the equals() method.
🌐
DevQA
devqa.io › java-compare-two-arraylists
Java Compare Two Lists
March 17, 2020 - import java.util.Arrays; import java.util.List; public class CompareTwoLists { public static void main(String[] args) { List<String> listOne = Arrays.asList("a", "b", "c"); List<String> listTwo = Arrays.asList("a", "b", "c"); List<String> listThree = Arrays.asList("c", "a", "b"); boolean isEqual = listOne.equals(listTwo); System.out.println(isEqual); isEqual = listOne.equals(listThree); System.out.println(isEqual); } }
🌐
Baeldung
baeldung.com › home › java › java list › finding the differences between two lists in java
Finding the Differences Between Two Lists in Java | Baeldung
April 29, 2026 - A Java Stream can be used for performing sequential operations on data from collections, which includes filtering the differences between lists: List<String> differences = listOne.stream() .filter(element -> !listTwo.contains(element)) .collect(Collectors.toList()); assertEquals(2, differences.size()); assertThat(differences).containsExactly("Tom", "John"); As in our first example, we can switch the order of lists to find the different elements from the second list: List<String> differences = listTwo.stream() .filter(element -> !listOne.contains(element)) .collect(Collectors.toList()); assertEquals(3, differences.size()); assertThat(differences).containsExactly("Daniel", "Alan", "George"); We should note that the repeated calling of List.contains() can be a costly operation for larger lists.
🌐
Javatpoint
javatpoint.com › how-to-compare-two-arraylist-in-java
How to Compare Two ArrayList in Java - Javatpoint
Java Program to swap two string variables without using third or temp variable.
Find elsewhere
🌐
Javaprogramto
javaprogramto.com › 2020 › 04 › how-to-compare-two-arraylist-for-equality-in-java.html
How to compare two ArrayList for equality in Java 8? ArrayList equals() or containsAll() methods works?
June 13, 2021 - If even anyone's value is different anyone list will return false as a result. java compare two lists of objects: package com.java.w3schools.blog.arraylist; import java.util.ArrayList; import java.util.List; /** * * Example compare two ArrayList for equality in Java. * * @author javaprogramto.com * */ public class TwoArrayListEqualityExample { public static void main(String[] args) { // list 1 List<String> listOne = new ArrayList<String>(); listOne.add("super"); listOne.add("this"); listOne.add("overlaoding"); listOne.add("overriding"); // list 2 List<String> listTwo = new ArrayList<String>(); listTwo.add("super"); listTwo.add("this"); listTwo.add("overlaoding"); listTwo.add("overriding"); // comparing two lists boolean isEqual = listOne.equals(listTwo); System.out.println(isEqual); } } Output: true ·
Top answer
1 of 5
5

Perhaps the easiest solution would be to use two List<List<String>>s instead. Assuming the List implementations used extend AbstractList, using equals will give you the desired behavior. From the documentation for AbstractList.equals:

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.

You can easily wrap a String[] in a thin List<String> implementation that extends AbstractList by using Arrays.asList.

EDIT: Here's an example:

String[] array1 = {"1", "2", "3"};
String[] array2 = {"4", "7"};

String[] array3 = {"1", "2", "3"};
String[] array4 = {"4", "7"};

List<List<String>> lst1 = new ArrayList<>();
lst1.add(Arrays.asList(array1));
lst1.add(Arrays.asList(array2));

List<List<String>> lst2 = new ArrayList<>();
lst2.add(Arrays.asList(array3));
lst2.add(Arrays.asList(array4));

System.out.println(lst1.equals(lst2)); //prints true
2 of 5
3

Typically you should avoid dealing with Arrays. they are ugly and lead to these kind of problems. If possible use List<List<String>> then you can use .equals() normally.

if you insist, you could use a custom isequal implementation like below. the key is to use Arrays.equals()

public class DemoEquals {
    List<String[]> listOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"});
    List<String[]> listOneOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"});
    List<String[]> listTwo = (List<String[]>) Arrays.asList(new String[]{"2one1", "2one2"}, new String[]{"2two1"});

    private boolean isEqual(List<String[]> list1, List<String[]> list2) {
        if (list1.size() != list2.size()) return false;
        for (int i = 0; i < list1.size(); i++) {
            if (!Arrays.equals(list1.get(i), list2.get(i))) return false;
        }
        return true;
    }
    @SuppressWarnings("unchecked")
    private void isEqual() {
        //prints true
        System.out.println(isEqual(Collections.EMPTY_LIST, Collections.EMPTY_LIST));
        //prints true
        System.out.println(isEqual(listOne, listOne));
        //prints true
        System.out.println(isEqual(listOne, listOneOne));
        //prints false
        System.out.println(isEqual(listOne, listTwo));
        //prints false
        System.out.println(isEqual(listOne, Collections.EMPTY_LIST));

    }
    public static void main(String[] args) {
        new DemoEquals().isEqual();
    }
}
🌐
W3docs
w3docs.com › home › code snippets › java › java compare two lists
Java Compare Two Lists | W3docs
Here is an example of how to compare two lists in Java: import java.util.List; import java.util.Arrays; List<String> list1 = Arrays.asList("a", "b", "c"); List<String> list2 = Arrays.asList("a", "b", "c"); if (list1.equals(list2)) { System.out.println("Lists are equal"); } else { System.out.println("Lists are not equal"); } Copy ·
🌐
Stack Overflow
stackoverflow.com › questions › 62020649 › java-compare-two-lists-of-string-of-same-size-where-order-is-important
Java compare two lists of String of same size where order is important - Stack Overflow
After the first elements in each list, because they are the same, it will find the distance between the current position in the first list and the index of the first occurrence of that element in the second list and the other way around. If the distances are equal then it will add space in the second list. So the result will be ... Sign up to request clarification or add additional context in comments. ... thanks you for the reply but it does not give me the same output. I corrected the list above in question .
Top answer
1 of 12
182

EDIT

Here are two versions. One using ArrayList and other using HashSet

Compare them and create your own version from this, until you get what you need.

This should be enough to cover the:

P.S: It is not a school assignment :) So if you just guide me it will be enough

part of your question.

continuing with the original answer:

You may use a java.util.Collection and/or java.util.ArrayList for that.

The retainAll method does the following:

Retains only the elements in this collection that are contained in the specified collection

see this sample:

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;

public class Repeated {
    public static void main( String  [] args ) {
        Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
        Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));

        listOne.retainAll( listTwo );
        System.out.println( listOne );
    }
}

EDIT

For the second part ( similar values ) you may use the removeAll method:

Removes all of this collection's elements that are also contained in the specified collection.

This second version gives you also the similar values and handles repeated ( by discarding them).

This time the Collection could be a Set instead of a List ( the difference is, the Set doesn't allow repeated values )

import java.util.Collection;
import java.util.HashSet;
import java.util.Arrays;

class Repeated {
      public static void main( String  [] args ) {

          Collection<String> listOne = Arrays.asList("milan","iga",
                                                    "dingo","iga",
                                                    "elpha","iga",
                                                    "hafil","iga",
                                                    "meat","iga", 
                                                    "neeta.peeta","iga");

          Collection<String> listTwo = Arrays.asList("hafil",
                                                     "iga",
                                                     "binga", 
                                                     "mike", 
                                                     "dingo","dingo","dingo");

          Collection<String> similar = new HashSet<String>( listOne );
          Collection<String> different = new HashSet<String>();
          different.addAll( listOne );
          different.addAll( listTwo );

          similar.retainAll( listTwo );
          different.removeAll( similar );

          System.out.printf("One:%s%nTwo:%s%nSimilar:%s%nDifferent:%s%n", listOne, listTwo, similar, different);
      }
}

Output:

$ java Repeated
One:[milan, iga, dingo, iga, elpha, iga, hafil, iga, meat, iga, neeta.peeta, iga]

Two:[hafil, iga, binga, mike, dingo, dingo, dingo]

Similar:[dingo, iga, hafil]

Different:[mike, binga, milan, meat, elpha, neeta.peeta]

If it doesn't do exactly what you need, it gives you a good start so you can handle from here.

Question for the reader: How would you include all the repeated values?

2 of 12
45

You can try intersection() and subtract() methods from CollectionUtils.

intersection() method gives you a collection containing common elements and the subtract() method gives you all the uncommon ones.

They should also take care of similar elements

🌐
Coderanch
coderanch.com › t › 648745 › java › compare-ArrayLists-type-ArrayList-String
how to compare two ArrayLists of type ArrayList<String[]>? (Java in General forum at Coderanch)
April 16, 2015 - JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility ... You can also go through the methods of the List interface and you will probably find exactly what you want already there. ... You could have an outer loop that loops through text and an inner loop that loops through orgId and inside that, compare the two items to see if they are equal.