2023. 2. 6. 11:24ใJAVA/Effective JAVA
item14. Comparable์ ๊ตฌํํ ์ง ๊ณ ๋ฏผํ๋ผ.
compareTo
: compareTo๋ Comparable ์ธํฐํ์ด์ค์ ์ ์ผํ ๋ฉ์๋๋ก Objet.equals(๋จ์ ๋์น์ฑ)์ ๋ํด์ ์์๊น์ง ๋น๊ตํ ์ ์์ผ๋ฉฐ Generic์ ์ง์ํ๋ค.
Comparable์ ๊ตฌํํ๋ค๋ ๊ฒ์ ๊ทธ ํด๋์ค์ ์ธ์คํด์ค๋ค์๋ ์์ฐ์ ์ธ ์์ natural order๊ฐ ์์์ ๋ปํ๋ค.
์ํ๋ฒณ์ด๋ ์ซ์, ์ฐ๋์ ๊ฐ์ด ์์๊ฐ ๋ช ํํ ๊ฐ ํด๋์ค๋ฅผ ์์ฑํ๋ค๋ผ๋ฉด ๋ฐ๋์ Comparable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์.
compareTo ์ผ๋ฐ ๊ท์ฝ
BigDecimal n1 = BigDecimal.valueOf(23134134);
BigDecimal n2 = BigDecimal.valueOf(11231230);
BigDecimal n3 = BigDecimal.valueOf(53534552);
BigDecimal n4 = BigDecimal.valueOf(11231230);
1๏ธโฃ Comparable์ ๊ตฌํํ ํด๋์ค๋ ๋ชจ๋ x,y์ ๋ํด sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) ์ฌ์ผ ํ๋ค.
-> ๋ ๊ฐ์ฒด์ ์ฐธ์กฐ์ ์์๋ฅผ ๋ฐ๊ฟ ๋น๊ตํด๋ ์์ํ ๊ฒฐ๊ณผ๊ฐ ๋์์ผ ํ๋ค.
// p.88 ๋์นญ์ฑ
System.out.println("n1.compareTo(n2) = " + n1.compareTo(n2));
System.out.println("n2.compareTo(n1) = " + n2.compareTo(n1));
์ฆ, ์ฒซ ๋ฒ์งธ ๊ฐ์ฒด๊ฐ ๋ ๋ฒ์งธ ๊ฐ์ฒด๋ณด๋ค ์์ผ๋ฉด, ๋ ๋ฒ์งธ๊ฐ ์ฒซ ๋ฒ์งธ๋ณด๋ค ์ปค์ผ ํ๋ค.
์ฒซ ๋ฒ์งธ๊ฐ ๋ ๋ฒ์งธ์ ํฌ๊ธฐ๊ฐ ๊ฐ๋ค๋ฉด, ๋ ๋ฒ์งธ๋ ์ฒซ ๋ฒ์งธ์ ๊ฐ์์ผ ํ๋ค.
์ฒซ ๋ฒ์งธ๊ฐ ๋ ๋ฒ์งธ๋ณด๋ค ํฌ๋ฉด, ๋ ๋ฒ์งธ๋ ์ฒซ ๋ฒ์งธ๋ณด๋ค ์์์ผ ํ๋ค.
2๏ธโฃ Comparable์ ๊ตฌํํ ํด๋์ค๋ ์ถ์ด์ฑ์ ๋ณด์ฅํด์ผ ํ๋ค. x.compareTo(y) > 0 && y.compareTo(z) ์ด๋ฉด x.compareTo(z) ์ฌ์ผ ํ๋ค.
-> ์ฒซ ๋ฒ์งธ๊ฐ ๋ ๋ฒ์งธ๋ณด๋ค ํฌ๊ณ ๋ ๋ฒ์งธ๊ฐ ์ธ ๋ฒ์งธ๋ณด๋ค ํฌ๋ฉด, ์ฒซ ๋ฒ์งธ๋ ์ธ ๋ฒ์งธ๋ณด๋ค ์ปค์ผ ํ๋ค๋ ๋ป์ด๋ค.
// p.89 ์ถ์ด์ฑ
System.out.println("n3.compareTo(n1) > 0 = " + (n3.compareTo(n1) > 0));
System.out.println("n1.compareTo(n2) > 0 = " + (n1.compareTo(n2) > 0));
System.out.println("n3.compareTo(n2) > 0 = " + (n3.compareTo(n2) > 0));
3๏ธโฃ Comparable์ ๊ตฌํํ ํด๋์ค๋ ๋ชจ๋ z์ ๋ํด x.compareTo(y) == ์ด๋ฉด sgn(x.compareTo(z)) == sgn(y.compareTo(z))์ด๋ค.
-> ํฌ๊ธฐ๊ฐ ๊ฐ์ ๊ฐ์ฒด๋ค๋ผ๋ฆฌ๋ ์ด๋ค ๊ฐ์ฒด์ ๋น๊ตํ๋๋ผ๋ ํญ์ ๊ฐ์์ผ ํ๋ค.
// p.89 ์ผ๊ด์ฑ
System.out.println("n4.compareTo(n2) = " + n4.compareTo(n2));
System.out.println("n2.compareTo(n1) = " + n2.compareTo(n1));
System.out.println("n4.compareTo(n1) = " + n4.compareTo(n1));
4๏ธโฃ (x.compareTo(y) == 0) == (x.equals(y)) ์ฌ์ผ ํ๋ค. (๊ถ๊ณ ๊ฐ ํ์๋ ์๋์ง๋ง ๊ผญ ์งํค๋๊ฒ ์ข๋ค.)
Comparable์ ๊ตฌํํ๊ณ ์ด ๊ถ๊ณ ๋ฅผ ์งํค์ง ์๋ ๋ชจ๋ ํด๋์ค๋ ๊ทธ ์ฌ์ค์ ๋ช ์ํด์ผ ํ๋ค.
-> compareTo ๋ฉ์๋๋ก ์ํํ ๋์น์ฑ ํ ์คํธ์ ๊ฒฐ๊ณผ๊ฐ equals์ ๊ฐ์์ผ ํ๋ค.
// p.89 compareTo๊ฐ 0์ด๋ผ๋ฉด equals๋ true์ฌ์ผ ํ๋ค. (์๋ ์๋ ์๊ณ ...)
BigDecimal oneZero = new BigDecimal("1.0");
BigDecimal oneZeroZero = new BigDecimal("1.00");
System.out.println("oneZero.compareTo(oneZeroZero) = " + oneZero.compareTo(oneZeroZero)); // Tree, TreeMap
System.out.println("oneZero.equals(oneZeroZero) = " + oneZero.equals(oneZeroZero)); // ์์๊ฐ ์๋ ์ปฌ๋ ์
-> ์ด ์์ ๋ฅผ ์คํํ๋ฉด
์ด๋ ๊ฒ equals์ compareTo์ ๊ฒฐ๊ณผ๊ฐ ๊ฐ์ง ์์ ๊ฒ์ ํ์ธํ ์ ์๋ค.
BigDecimal ํด๋์ค๋ compareTo์ equals๊ฐ ์ผ๊ด๋์ง ์์ ํด๋์ค๋ก ๋น HashMap ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ค์์ new BigDecimal("1.0"), new Decimal("1.00")์ ์ฐจ๋ก๋ก ์ถ๊ฐํ๋ฉด ์ด ๋ BigDecimal์ equals ๋ฉ์๋๋ก ๋น๊ตํ๋ฉด ์๋ก ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ HashSet์ ๋๊ฐ์ ์์๋ฅผ ๊ฐ์ง๋ง,
HashSet ๋์ ์ TreeSet์ ์ฌ์ฉํ๋ฉด ์์๋ฅผ ํ๋๋ง ๊ฐ๊ฒ ๋๋ค. compareTo ๋ฉ์๋๋ก ๋น๊ตํ๋ฉด ๋ BigDecimal ์ธ์คํด์ค๊ฐ ๋๊ฐ๊ธฐ ใธ๋งคใ ใด์ด๋ค.
/**
* Compares this {@code BigDecimal} with the specified
* {@code Object} for equality. Unlike {@link
* #compareTo(BigDecimal) compareTo}, this method considers two
* {@code BigDecimal} objects equal only if they are equal in
* value and scale (thus 2.0 is not equal to 2.00 when compared by
* this method).
*
* @param x {@code Object} to which this {@code BigDecimal} is
* to be compared.
* @return {@code true} if and only if the specified {@code Object} is a
* {@code BigDecimal} whose value and scale are equal to this
* {@code BigDecimal}'s.
* @see #compareTo(java.math.BigDecimal)
* @see #hashCode
*/
@Override
public boolean equals(Object x) {
if (!(x instanceof BigDecimal))
return false;
BigDecimal xDec = (BigDecimal) x;
if (x == this)
return true;
if (scale != xDec.scale)
return false;
long s = this.intCompact;
long xs = xDec.intCompact;
if (s != INFLATED) {
if (xs == INFLATED)
xs = compactValFor(xDec.intVal);
return xs == s;
} else if (xs != INFLATED)
return xs == compactValFor(this.intVal);
return this.inflated().equals(xDec.inflated());
}
(thus 2.0 is not equal to 2.00 when compared by this method).
๋ผ๊ณ ์๋ ค์ฃผ๊ณ ์๋ค.