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)

Java相关代码片段

add border to pane in javafx

how to check response time using REST Assured

java home path in mac

my image is not found java fx

pom.xml JUnit 5 dependencies

java set difference

regex for first name only

StringBuffer(CharSequence seq)

array class methods in java

how to read all line from file java

Java int length

intent example in android

how to doubling size of an arrays in java

how to find my jdk path

how to handle multiple throws exception in java

spring boot jdbc for postgrest

factorial of a number

types of typecasting in java

types of assignment statement in java

Thread mutex

env files in spring boot

java over loading

logging in spring boot

how to split a list in multiple lists java

can two servlet have same urlpattern

spring debug

jsp spring

hanoi tower recursion java

java equals

age difference java

sed cheat sheet

java compare method

how to make a activity default in manifest

java max memory size

yum uninstall java

timestamp to long java

how to set background image in android studio

simple calculator in android

When to use HashMap vs Map

spring boot jpa

spring data jpa

spring jdbc

jpa vs hibernate

java with checking the password matching

profile in spring boot

string templates java

spring rest controller

java arraylist get item

com.oracle.jdbc

check if map is not empty java

how to install java 8 on debian 12

regex caractères spéciaux java

java 11 yum install

manage session in redis spring boot

java: error: release version 19 not supported

bean scope in spring

xml configuration in spring

spring cdi annotations

postconstruct and predestroy in spring

java decode_message

spring lazy initialization

java decorator design pattern

dependency injection in spring

check back pressed in fragment andorid

send email to any domain using java

record java

vs code setup for input/output in java

substring java

receive second word in string java

loose coupling in java

default value of char in java

spring boot repository pattern

spring boot h2 database

add security to spring admin

java islessthan method

Java implementation of recursive Binary Search

java round double

java downcasting

range of fibonacci series in java using recursion

java by anyone