-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
item-11 equals 를 재정의하려거든 hashCode 도 재정의하라
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package item11; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @author lokie on 2020/07/06 | ||
*/ | ||
public final class PhoneNumber { | ||
private final short areaCode, prefix, lineNum; | ||
|
||
public PhoneNumber(int areaCode, int prefix, int lineNum) { | ||
this.areaCode = rangeCheck(areaCode, 999, "지역코드"); | ||
this.prefix = rangeCheck(prefix, 999, "프리픽스"); | ||
this.lineNum = rangeCheck(lineNum, 9999, "가입자 번호"); | ||
} | ||
|
||
private static short rangeCheck(int val, int max, String arg) { | ||
if (val < 0 || val > max) { | ||
throw new IllegalArgumentException(arg + ": " + val); | ||
} | ||
return (short) val; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (!(obj instanceof PhoneNumber)) { | ||
return false; | ||
} | ||
PhoneNumber pn = (PhoneNumber) obj; | ||
return pn.lineNum == lineNum && pn.prefix == prefix | ||
&& pn.areaCode == areaCode; | ||
} | ||
|
||
private int hashCode; | ||
|
||
@Override | ||
public int hashCode() { | ||
// hashCode 작성법 | ||
// int result = Short.hashCode(areaCode); | ||
// result = 31 * result + Short.hashCode(prefix); | ||
// result = 31 * result + Short.hashCode(lineNum); | ||
// return result; | ||
|
||
// Objects 클래스가 제공하는 hashCode 반환 메서드 | ||
// return Objects.hash(areaCode, prefix, lineNum); | ||
|
||
// 해시코드 캐싱 | ||
int result = hashCode; | ||
if (result == 0) { | ||
result = Short.hashCode(areaCode); | ||
result = 31 * result + Short.hashCode(prefix); | ||
result = 31 * result + Short.hashCode(lineNum); | ||
hashCode = result; | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package item11; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author lokie on 2020/07/06 | ||
*/ | ||
public class Test { | ||
static Map<Integer, String> map = new HashMap(); | ||
public static void a(){ | ||
Integer a = new Integer(150); | ||
System.out.println(System.identityHashCode(a)); | ||
map.put(a,"150"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
a(); | ||
Integer a = new Integer(150); | ||
Integer b = new Integer(150); | ||
System.out.println(System.identityHashCode(b)); | ||
System.out.println(map.get(b)); | ||
System.out.println(a == b); | ||
System.out.println(a.equals(b)); | ||
} | ||
} |