Effective Java Chapter 3

Chapter 3. Methods common to all objects

10. equals overrides should be subject to general contracts

11. If you override equals, also override hashcode

12. Always override toString

    public String toString() {
        ThreadGroup group = getThreadGroup();
        if (group != null) {
            return "Thread[" + getName() + "," + getPriority() + "," +
                           group.getName() + "]";
        } else {
            return "Thread[" + getName() + "," + getPriority() + "," +
                            "" + "]";
        }
    }

13. Be careful when overriding clone

14. Consider implementing Comparable

package tryAny.effectiveJava;

import static java.util.Comparator.*;
import java.util.Comparator;
import java.util.stream.Stream;

public class CompareTest {
    public static void main(String[] args) {
	Stream<PhoneNum> s = Stream.of(new PhoneNum(111, 222, 333), new PhoneNum(111, 222, 222),
		new PhoneNum(111, 333, 111), new PhoneNum(000, 999, 1));
	s.sorted().forEach(System.out::println);
    }
}

class PhoneNum implements Comparable<PhoneNum> {
    int areaCode;
    int prefix;
    int lineNum;

    public PhoneNum(int areaCode, int prefix, int lineNum) {
	this.areaCode = areaCode;
	this.prefix = prefix;
	this.lineNum = lineNum;
    }

    private static final Comparator<PhoneNum> COMPARATOR = comparingInt((PhoneNum pn) -> pn.areaCode)
	    .thenComparingInt(pn -> pn.prefix).thenComparingInt(pn -> pn.lineNum);

    @Override
    public int compareTo(PhoneNum pn) {
	return COMPARATOR.compare(this, pn);
    }

    @Override
    public String toString() {
	StringBuilder sb = new StringBuilder();
	sb.append("areaCode:").append(areaCode).append(",prefix:").append(prefix).append(",lineNum:").append(lineNum);
	return sb.toString();
    }
}

Recommended Posts

Effective Java Chapter 2
Effective Java Chapter 6 34-35
Effective Java Chapter 4 15-22
Effective Java Chapter 3
Effective Java 3rd Edition Chapter 5 Generics
Effective Java 3rd Edition Chapter 8 Methods
[Read Effective Java] Chapter 2 Item 7 "Avoid Finalizers"
Effective Java 3rd Edition Chapter 9 Program General
Java Performance Chapter 1 Introduction
effective java 3rd summary
Builder pattern (Effective Java)
Effective Java 3rd Edition Chapter 6 enums and annotations
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
[Read Effective Java] Chapter 3 Item 10 "Always Override toString"
Effective Java 3rd Edition Chapter 7 Lambda and Streams
I started Java Gold (Chapter 1-1)
[Read Effective Java] Chapter 3 Item 12 "Considering Implementation of Comparable"
Java Performance Chapter 2 Performance Testing Approach
Java
Effective Java 3rd Edition Chapter 2 Object Creation and Disappearance
Java
From Ineffective Java to Effective Java
[Effective Java] Remove obsolete object references
Java Performance Chapter 5 Garbage Collection Basics
[Effective Java] Avoid creating unnecessary objects
[Read Effective Java] Chapter 2 Item 4 "Force uninstantiation with a private constructor"
[Read Effective Java] Chapter 3 Item 9 "When overriding equals, always override hashCode"
[Read Effective Java] Chapter 2 Item 5 "Avoid the creation of unnecessary objects"
Java learning (0)
[Java] array
Java protected
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
Java array
[Java] ArrayDeque
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
[Read Effective Java] Chapter 2 Item 1 "Consider static factory methods instead of constructors"
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism