[Read Effective Java] Chapter 3 Item 12 "Considering Implementation of Comparable"

Consider implementing Comparable

Unlike other methods, the compareTo method is not declared in Object. To be precise, the compareTo method is the only method in the Compareable interface. If you're writing a value class that has a clearly natural order, such as alphabetical, numerical, or chronological, you'll benefit from a little effort, so implement Comparable.

Sample code

public int compareTo(PhoneNumber pn) {
    //Compare area code
    if (areaCode < pn.areaCode)
        return -1;
    if (areaCode > pn.areaCode)
        return 1;

    //Area code is equal and compares the first half of the city code
    if (prefix < pn.prefix)
        return -1;
    if (prefix > pn.prefix)
        return 1;

    //The area code and the first half of the city code are equal, and the second half of the city code is compared.
    if (lineNumber < pn.lineNumber)
        return -1;
    if (lineNumber > pn.lineNumber)
        return 1;
    
    return 0; //All fields are equal
}

Recommended Posts

[Read Effective Java] Chapter 3 Item 12 "Considering Implementation of Comparable"
[Read Effective Java] Chapter 2 Item 7 "Avoid Finalizers"
[Read Effective Java] Chapter 2 Item 5 "Avoid the creation of unnecessary objects"
[Read Effective Java] Chapter 3 Item 10 "Always Override toString"
[Read Effective Java] Chapter 2 Item 1 "Consider static factory methods instead of constructors"
[Read Effective Java] Chapter 2 Item 6 "Remove obsolete object references"
Effective Java Chapter 2
[Read Effective Java] Chapter 2 Item 4 "Force uninstantiation with a private constructor"
Effective Java Chapter 6 34-35
[Read Effective Java] Chapter 3 Item 9 "When overriding equals, always override hashCode"
Effective Java Chapter 4 15-22
Effective Java Chapter 3
[Read Effective Java] Chapter 2 Item 2 "Consider a builder when faced with a large number of constructor parameters"
[Read Effective Java] Chapter 3 Item 8 "When overriding equals, follow the general contract"
[Read Effective Java] Chapter 2 Item 3 "Force singleton characteristics with private constructor or enum type"
[Java] Implementation of Faistel Network
Implementation of gzip in java
Implementation of tri-tree in Java
Effective Java 3rd Edition Chapter 5 Generics
Effective Java 3rd Edition Chapter 8 Methods
Implementation of like function in Java
Implementation of clone method for Java Record
Implementation of DBlayer in Java (RDB, MySQL)
Effective Java 3rd Edition Chapter 9 Program General
Effective Java 3rd Edition Chapter 6 enums and annotations
Do you need a memory-aware implementation of Java?
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
Effective Java 3rd Edition Chapter 7 Lambda and Streams
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
[Java / Spring Boot] Spring security ④ --Implementation of login process
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
[Java / Kotlin] Resize considering the orientation of the image