Looks like from all the comments in here you dont need to use Comparator at all. Because:

1) You are using HashSet that does not work with Comparator. It is not ordered.

2) You just need to make sure that two HashSets containing Products are equal. It means they are same size and contain the same set of Products.

Since you already added hashCode and equals methods to Product all you need to do is call equals method on those HashSets.

HashSet<Product> set1 = ...
HashSet<Product> set2 = ...

assertTrue( set1.equals(set2) );
Answer from tsolakp on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › guide to implementing the compareto method
Guide to Implementing the compareTo Method | Baeldung
May 29, 2025 - TreeMap and TreeSet are two implementations from the Java Collections Framework that assist us with the automatic sorting of their elements. We may use objects that implement the Comparable interface in a sorted map or as elements in a sorted set. Let’s look at an example of a custom class that compares players based on the number of goals they have scored: @Override public int compareTo(FootballPlayer anotherPlayer) { return Integer.compare(this.goalsScored, anotherPlayer.goalsScored); }
🌐
Blogger
azagorneanu.blogspot.com › 2011 › 08 › how-to-generate-equals-hashcode.html
Thinking about IT: How to generate equals(), hashCode(), toString() and compareTo() using Apache Commons Lang in Eclipse
Still, we can configure Eclipse to generate equals(), hashCode() and compareTo() using Apache Commons Lang builders by using Eclipse templates. Templates are little pieces of code with defined placeholders. Each template has a name, which serves as a shortcut to the template itself. You type the name, press CTRL + SPACE and it will be expanded. In order to access Eclipse templates open Window -> Preferences and select Java -> Editor -> Templates.
🌐
Eclipse
bugs.eclipse.org › bugs › show_bug.cgi
302714 – generate java.lang.Comparable.compareTo() method
Bugzilla – Bug 302714 generate java.lang.Comparable.compareTo() method Last modified: 2010-02-12 09:26:33 EST ... This Bugzilla instance closed for new bug entry. Eclipse projects now use GitHub or Eclipse GitLab.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 8 )
1 week ago - {(x, y) such that x.compareTo(y) == 0}. It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method: ... This interface is a member of the Java Collections Framework.
Top answer
1 of 2
1

Looks like from all the comments in here you dont need to use Comparator at all. Because:

1) You are using HashSet that does not work with Comparator. It is not ordered.

2) You just need to make sure that two HashSets containing Products are equal. It means they are same size and contain the same set of Products.

Since you already added hashCode and equals methods to Product all you need to do is call equals method on those HashSets.

HashSet<Product> set1 = ...
HashSet<Product> set2 = ...

assertTrue( set1.equals(set2) );
2 of 2
0

This implementation does not seem to be consistent. You have no control over how the hash codes look like. If you have obj1 < obj2 according to compareTo in the first try, the next time you start your JVM it could be the other way around obj1 > obj2.

The only thing that you really know is that if diff == 0 then the objects are considered to be equal. However you can also just use the equals method for that check.

It is now up to you how you define when obj1 < obj2 or obj1 > obj2. Just make sure that it is consistent.

By the way, you know that the current implementation does not include ProductName name in the equals check? Dont know if that is intended thus the remark.

The question is, what do you know about that attributes? Maybe they implement Comparable (for example if they are Numbers), then you can order according to their compareTo method. If you totally know nothing about the objects, it will be hard to build up a consistent ordering.

If you just want them to be ordered consistently but the ordering itself does not play any role, you could just give them ids at creation time and sort by them. At this point you could indeed use the hashcodes if it does not matter that it can change between JVM calls, but only then.

🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... String myStr1 = "Hello"; String myStr2 = "Hello"; System.out.println(myStr1.compareTo(myStr2)); // Returns 0 because they are equal
🌐
Tutorialspoint
tutorialspoint.com › java › number_compareto.htm
Java - compareTo() Method
Java Vs. C++ ... The method compares the Number object that invoked the method to the argument. It is possible to compare Byte, Long, Integer, etc. However, two different types cannot be compared, both the argument and the Number object invoking ...
🌐
Quora
quora.com › What-does-the-compareto-function-do-in-Java
What does the compareto() function do in Java? - Quora
Below, I am giving a working MergeSort code. Just paste this code in Eclipse.Observe the use of compareTo(). //Start import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 72160928 › how-to-implement-compareto-method-in-java-and-what-does-it-mean
comparable - How to implement compareTo method in Java and what does it mean - Stack Overflow
This is where writing a custom (@Override) compareTo method comes in. By default the compareTo method returns either 1, 0 or -1. There is no inherent meaning to these numbers, in fact they can be any numbers you like (as long as they are different).
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 7 )
{(x, y) such that x.compareTo(y) == 0}. It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method: ... This interface is a member of the Java Collections Framework.
🌐
Zero To Mastery
zerotomastery.io › blog › java-compareto-method
Beginner's Guide To compareto In Java (With Code Examples) | Zero To Mastery
March 3, 2025 - It gets to 'e' in "Alice" and 'i' in "Alicia", and since 'e' comes before 'i', Java returns -1— meaning "Alice" comes first. However, if the words were exactly the same ("Alice".compareTo("Alice")), compareTo would return 0, because there’s nothing to compare!
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 17 & JDK 17)
January 20, 2026 - This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
🌐
Programiz
programiz.com › java-programming › library › string › compareto
Java String compareTo()
The compareTo() method takes a single parameter. ... class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "Learn Java"; String str3 = "Learn Kolin"; int result; // comparing str1 with str2
🌐
Eclipse Che
eclipsesource.com › blogs › 2012 › 07 › 17 › clean-compareto-methods-with-google-guava
Clean compareTo methods with Google Guava
As you can see we have a class called Fruit with three fields. In its compareTo method you can see that all three fields are used for comparison. I think this is a pretty common solution. The downside of this implementation is that every field can influence the compare result.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › compareto in java
compareTo in Java: A Complete Guide with Practice Exercises
March 17, 2025 - Build Tools (Optional): Get to know tools such as Maven or Gradle for managing projects, particularly if your project will involve various dependencies. ... The method compareTo() evaluates two strings in a lexicographical manner.
🌐
Javapractices
javapractices.com › topic › TopicAction.do
Java Practices->Implementing compareTo
When a class extends a concrete Comparable class and adds a significant field, a correct implementation of compareTo cannot be constructed. The only alternative is to use composition instead of inheritance. (See Effective Java for more information.)
🌐
CodeGym
codegym.cc › java blog › strings in java › java string compareto() method
Java String CompareTo() Method
December 25, 2024 - The java string class compareTo() method returns the 0 value if both strings are lexicographically equal. If the compared string is greater lexicographically then the positive value is returned otherwise the negative value is returned.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 11 & JDK 11 )
January 20, 2026 - {(x, y) such that x.compareTo(y) == 0}. It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method: ... This interface is a member of the Java Collections Framework.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-compareto-method-example
Java String compareTo() Method with examples
September 16, 2022 - Here we have three Strings and we are comparing them with each other using compareTo() method. public class JavaExample { public static void main(String args[]) { String str1 = "String method tutorial"; String str2 = "compareTo method example"; String str3 = "String method tutorial"; int var1 = str1.compareTo(str2); System.out.println("str1 and str2 comparison: "+var1); int var2 = str1.compareTo(str3); System.out.println("str1 and str3 comparison: "+var2); int var3 = str2.compareTo("compareTo method example"); System.out.println("str2 and string argument comparison: "+var3); } }
🌐
Mkyong
mkyong.com › home › java › java string compareto() examples
Java String compareTo() examples - Mkyong.com
January 24, 2022 - System.out.println("a".compare... System.out.println("11".compareTo("11234")); // -3 · The Java String compareToIgnoreCase() method compares two strings lexicographically, ignoring cases....