Just implement the method that iterates, and reuse it every time you need it:

public static <T> boolean areEqualIgnoringOrder(List<T> list1, List<T> list2, Comparator<? super T> comparator) {

    // if not the same size, lists are not equal
    if (list1.size() != list2.size()) {
        return false;
    }

    // create sorted copies to avoid modifying the original lists
    List<T> copy1 = new ArrayList<>(list1);
    List<T> copy2 = new ArrayList<>(list2);

    Collections.sort(copy1, comparator);
    Collections.sort(copy2, comparator);

    // iterate through the elements and compare them one by one using
    // the provided comparator.
    Iterator<T> it1 = copy1.iterator();
    Iterator<T> it2 = copy2.iterator();
    while (it1.hasNext()) {
        T t1 = it1.next();
        T t2 = it2.next();
        if (comparator.compare(t1, t2) != 0) {
            // as soon as a difference is found, stop looping
            return false;
        }
    }
    return true;
}
Answer from JB Nizet on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @QA-initi โ€บ how-to-compare-two-array-list-in-java-using-comparator-cc05617175b5
How to compare two array list in Java using Comparator | by QA-init | Medium
November 14, 2023 - ArrayList<Integer> list1 = new ... list2.add(1); Comparator<Integer> comparator = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }; Collections.sort(list1, comparator); ...
๐ŸŒ
ZetCode
zetcode.com โ€บ java โ€บ comparablecomparator
Java Comparable Comparator - comparing objects with Comparable and Comparator
Java Comparable and Comparator tutorial shows how to compare objects in Java with Comparable and Comparator interfaces. Comparing two objects is essential when doing sorting.
Discussions

Comparing two Arraylists
If you would like to compare the items of two lists. You can create a loop and then you can compare each item step by step. If you would like to compare two lists independently from each other. You can use Java Comparator class with sort function. You can define code logic which has sorting rules. With this way, you can compare the fields of class whatever you want. I hope this is your expected answer ? More on reddit.com
๐ŸŒ r/javahelp
5
3
January 25, 2018
comparator - java list of objects comparison - Stack Overflow
I am having List stud1 = new ArrayList (); and List stud2 = new ArrayList (); And Student class is having members like name, address. Wha... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 18, 2012
Comparing two comparator objects in Java - Stack Overflow
What does it mean for two comparators to be equal to one another? I feel this is an ambiguous concept for anything other than data classes, and I would be hesitant to use the Object.equals method for this. (For example, if by equality you mean "they will sort lists in the same order", then ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java Compare Two List's object values? - Stack Overflow
I have two list **ListA listA = new ArrayList ()** and ListB listB = new ArrayList () both contain object of type MyData and MyData contain these More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 2
10

Just implement the method that iterates, and reuse it every time you need it:

public static <T> boolean areEqualIgnoringOrder(List<T> list1, List<T> list2, Comparator<? super T> comparator) {

    // if not the same size, lists are not equal
    if (list1.size() != list2.size()) {
        return false;
    }

    // create sorted copies to avoid modifying the original lists
    List<T> copy1 = new ArrayList<>(list1);
    List<T> copy2 = new ArrayList<>(list2);

    Collections.sort(copy1, comparator);
    Collections.sort(copy2, comparator);

    // iterate through the elements and compare them one by one using
    // the provided comparator.
    Iterator<T> it1 = copy1.iterator();
    Iterator<T> it2 = copy2.iterator();
    while (it1.hasNext()) {
        T t1 = it1.next();
        T t2 = it2.next();
        if (comparator.compare(t1, t2) != 0) {
            // as soon as a difference is found, stop looping
            return false;
        }
    }
    return true;
}
2 of 2
4

Here's a Java 8 way of solving your problem. First make sure the lists are of equal length:

List<SystemUserWithNameAndId> list1 = ... ;
List<SystemUserWithNameAndId> list2 = ... ;

if (list1.size() != list2.size()) {
    return false;
}

Now build a Comparator using the new comparator utilities. The idea is that instead of writing custom logic for a comparator, most comparators do something like comparing two objects by extracting a key from them, and then comparing the keys. That's what this does.

Comparator<SystemUserWithNameAndId> comp =
    Comparator.comparingInt(SystemUserWithNameAndId::getSystemUserId);

Sort the lists. Of course, you might want to make copies before sorting if you don't want your function to have the side effect of sorting its input. If your input lists aren't random access (who uses LinkedList nowadays?) you might also want to copy them to ArrayLists to facilitate random access.

list1.sort(comp);
list2.sort(comp);

Run a stream over the indexes of the lists, calling the comparator on each pair. The comparator returns 0 if the elements are equals according to this comparator. If this is true for all pairs of elements, the lists are equal.

return IntStream.range(0, list1.size())
    .allMatch(i -> comp.compare(list1.get(i), list2.get(i)) == 0);
๐ŸŒ
HappyCoders.eu
happycoders.eu โ€บ java โ€บ comparator-comparable-compareto
compareTo, Comparable, Comparator - Comparing Java Objects
June 12, 2025 - To sort two objects by an order other than their natural order (or to sort objects of classes that do not implement Comparable at all), we have to use the java.util.Comparator interface.
๐ŸŒ
Quora
quora.com โ€บ How-can-two-lists-of-objects-be-compared-in-Java
How can two lists of objects be compared in Java? - Quora
Answer (1 of 4): Aฬฒlฬฒrฬฒiฬฒgฬฒhฬฒtฬฒ ฬฒ,ฬฒ ฬฒsฬฒoฬฒ ฬฒyฬฒoฬฒuฬฒ ฬฒwฬฒaฬฒnฬฒnฬฒaฬฒ ฬฒcฬฒoฬฒmฬฒpฬฒarฬฒeฬฒ ฬฒtฬฒwฬฒoฬฒ ฬฒlฬฒisฬฒtฬฒsฬฒ ฬฒoฬฒfฬฒ ฬฒoฬฒbฬฒjฬฒeฬฒcฬฒtฬฒs ฬฒiฬฒnฬฒ ฬฒJฬฒaฬฒvฬฒaฬฒโ€”ฬฒfฬฒiฬฒrฬฒsฬฒtฬฒ ฬฒtฬฒhฬฒiฬฒnฬฒgฬฒ ฬฒtฬฒoฬฒ ฬฒcฬฒhฬฒeฬฒcฬฒkฬฒ ฬฒiฬฒsฬฒ ฬฒiฬฒfฬฒ ฬฒtฬฒhฬฒeฬฒyฬฒโ€™ฬฒrฬฒeฬฒ ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-compare-objects-with-custom-comparator-in-java-419620
How to compare objects with custom comparator in Java | LabEx
In Java, a Comparator is an interface that allows you to define custom comparison logic for objects. It provides a way to compare two objects and determine their order, which is particularly useful when you want to sort collections or implement custom sorting mechanisms.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ javas-objects-compare-method-explained-7a9ccda5ea74
Javaโ€™s Objects.compare() Method Explained | Medium
December 13, 2024 - However, other approaches can achieve similar results depending on the requirements of the code. The Comparator interface provides a compare() method that can be directly used to compare two objects.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
The Comparator interface allows you to create a class with a compare() method that compares two objects to decide which one should go first in a list.
Top answer
1 of 2
3

You're calling the equals method on this line:

if (comparator.equals(myClass.getComparator())

Since you haven't defined this method explicitly on your Comparator class (which is an anonymous inner class), this defaults to the version inherited from Object - which considers two references equal only if they are the exact same object.

And your getComparator() method states return new Comparator() { ... }, so it's calling the constructor and creating a new object each time it's called. Thus the result of one call to getComparator will be a distinct object, and hence will not be considered equal to, the result of another call.

I can think of two possible ways to change your code so that the equality test returns true:

  1. Create the comparator only once, and return this same object from getComparator. This would involve a change somewhat like the following in myClass:

        private static Comparator<ListItem> cmp = new Comparator<myOtherObject>() {
           public int compare(myOtherObjectitem1, myOtherObjectitem2) {
                return (Integer.valueOf(myOtherObject.getRating()).compareTo(Integer.valueOf(myOtherObject.getRating())));
            }
        };
    
        static Comparator<ListItem> getComparator() {
            return cmp;
        }
    
  2. Provide an explicit equals() implementation (and thus a hashCode() one too, ideally). You can then control exactly which objects are considered equal to one of your comparators. This might be much easier if you define a concrete class for your comparator rather than it being an anonymous inner class.


At the end of the day, though, I fear your approach might not be right. What does it mean for two comparators to be equal to one another? I feel this is an ambiguous concept for anything other than data classes, and I would be hesitant to use the Object.equals method for this.

(For example, if by equality you mean "they will sort lists in the same order", then I'd add a method to your comparator class called isEquivalentSortOrder or something similar. This way you can specify exactly what you mean without having to rely on the woolly definition of "being the same".)

2 of 2
0

Why not to create inside myClass static variable of Comparator like:

class myClass{
    public static Comparator<ListItem> = new Comparator<myOtherObject>() {
        public int compare(myOtherObjectitem1, myOtherObjectitem2) {
            ...
        }
    };
}
๐ŸŒ
Medium
medium.com โ€บ @leninstalinesec โ€บ how-to-comparing-two-java-objects-5344f3d0fbc
How to Comparing two java objects | by lenin stalin | Medium
May 11, 2019 - More get with Java collection example ... class NameComparator implements Comparator<Person> { @Override public int compare(Person o1, Person o2) { return o1.name.compareTo(o2.name); } }class AgeComparator implements Comparator<Person> { @Override public int compare(Person o1, Person o2) { return ((Integer)o1.age).compareTo(o2.age); } } Now we can specify one of the above-defined sorting function to choose sorting on demand: ... List<Person> students = Arrays.asList( new Person(20,"Bob"), new Person(19, "Jane"), new Person(21,"Foo") ); Collections.sort(students, new AgeComparator());
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ -efficiently-compare-two-lists-objects-java
How to Efficiently Compare Two Lists of Objects in Java? - CodingTechRoom
Comparing two lists of objects in Java can be achieved through various techniques, depending on the desired outcome, such as equality check, content comparison, or structural comparison. Below are the common approaches. ... // Using Java Streams for unordered list comparison boolean areContentEqual = list1.size() == list2.size() && list2.containsAll(list1); ... Use List.equals(Object obj) method for simple equality checks if order matters.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java classes โ€บ java comparator and comparable
Java Comparator and Comparable
January 14, 2025 - We need to implement only one interface method: compare(). It takes two Car objects as inputs and compares their maximum speeds in the usual way (by subtraction). Like compareTo(), it returns an int, and the principle of comparison is the same. How do we use this? It's all straightforward: import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<Car> cars = new ArrayList<>(); Car ferrari = new Car(1990, "Ferrari 360 Spider", 310); Car lambo = new Car(2012, "Lamborghini Gallardo", 290); Car bugatti = new Car(2010, "Bugatti Veyron", 350); cars.add(ferrari); cars.add(bugatti); cars.add(lambo); Comparator speedComparator = new MaxSpeedCarComparator(); Collections.sort(cars, speedComparator); System.out.println(cars); } } Console output:
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2011 โ€บ 06 โ€บ comparator-and-comparable-in-java.html
Javarevisited: How to use Comparator and Comparable in Java? With example
While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. 3) If you see the logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 404400 โ€บ java โ€บ compare-objects-comparator
How to compare to objects using comparator? (Beginning Java forum at Coderanch)
FooComparator fc = ... if (fc.compare(aFoo, anotherFoo) > 0) ... The main reason Comparator is especially useful is that there is often more than one way to compare instances of a single class: you might sort Person objects by name, by address, by age... so you can define a NameComparator, an AddressComparator, and an AgeComparator. If you have Person implement Comparable, then you can only do it one way. A lot of the Java Collections classes let you supply a Comparator to tell the collection how to sort the objects you put into the collection.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ comparing-two-arraylist-in-java
Comparing two ArrayList In Java - GeeksforGeeks
January 23, 2026 - To compare two ArrayList objects, Java provides the equals() method. This method checks whether both lists have the same size and contain the same elements in the same order, making it a simple and reliable way to compare lists.
๐ŸŒ
Wikibooks
en.wikibooks.org โ€บ wiki โ€บ Java_Programming โ€บ Comparing_Objects
Comparing Objects - Wikibooks, open books for an open world
In Java, there are several existing methods that already sort objects from any class like Collections.sort(List<T> list). However, Java needs to know the comparison rules between two objects.