public class YourClass{ private int member1; private int member2; public YourClass(int member1, int member2){ this.member1 = member1; this.member2 = member2; } // You need to override the default equals mthod of Object class @Override public boolean equals(Object obj){ if (this == obj) // if they are the same object { return true; } else if (obj instanceof YourClass) { // if they are different objects but members have same value var otherObj = (YourClass) obj; // downcast to your class return otherObj.member1 = member1 && otherObj.member2 = member2; } return false; } } // Override hash to see whether they are same objects @Override public int hashCode(){ return Objects.hash(member1,member2); } public class Main { public static void main(String[] args) { var a = new YourClass(1,2); var b = new YourClass(1,2); System.out.println(a.equals(b)); // Are a and b same object? System.out.println(a.hashCode()); System.out.println(b.hashCode()); }}/* First, we need a base class that we want to use for comparing. */ public class Message{ private final String content; public Message(String content) { this.content = content; } public String getContent() { return content; } @Override public String toString() { return content; } } /* Second, we need a class that implements the way we compare objects from our base class. In this case, we compare objects by the length of the 'content' field. */ public class MessageComparator implements Comparator<Message> { @Override public int compare(Message m1, Message m2) { int m1Length = m1.getContent().length(); int m2Length = m2.getContent().length(); return Integer.compare(m1Length, m2Length); } } /* Class with main method where we have collection of our base class objects that we want to compare */ public class Main { public static void main(String[] args) { List<Message> messages = new ArrayList<>(); messages.add(new Message("Hello")); messages.add(new Message("humans!")); messages.add(new Message("We")); messages.add(new Message("came")); messages.add(new Message("in")); messages.add(new Message("peace!")); // Triggers the compare mechanism // Will sort by the length of 'content' field messages.sort(new MessageComparator()); messages.forEach(System.out::println); } } /* TIPS: If we have more fields in our base class, we can create more classes who implements Comparator<Message> interface. In that way, we could compare objects of our base class by multiple parameters (fields) *///To compare e.g. "Articles" first by their size and than by their title //we can override compareTo() methond in this way: @Override public int compareTo(Article otherArticle) { return Comparator.comparing(Article::getSize) .thenComparing(Article::getTitle) .compare(this, otherArticle); } //Other way would be more manual approach: @Override public int compareTo(Article otherArticle) { int res = Integer.valueOf(getSize()).compareTo(otherArticle.getSize()); if (res == 0) { res = String.valueOf(getTitle()).compareTo(otherArticle.getTitle()); } return res; }this.equals(other)