Spring 은 아니고 Java HashCode 관련 문의 하나 드려도 될까요?

110 views
Skip to first unread message

심준보

unread,
Dec 13, 2016, 9:07:44 PM12/13/16
to Korea Spring User Group Q&A


string 객체는 문자열이 같으면 동일 주소값을 반환 하기 때문에 HashCode 가 같은걸로 알고 있습니다.

그런데 서로다른 프로세스(물리적으로 다른 컴퓨터) 에서도 동일한 문자열은 동일한 HashCode가 반환 될까요?

제 생각은 참조하는 주소값이 달라지기 때문에 HashCode 또한 달라질 것이라고 생각되는데요.

그렇다면 서로다른 프로세스 내에서도 동일한 문자열에 대한 HashCode 를 동일한 값으로 얻기위해서는 어떤 처리를 해야되는지?

(예를 들어 내부적으로 오버라이딩을 통해 해싱 알고리즘을 맞춰 준다든가...)

조언좀 주시면 감사하겠습니다.

Spring과 좀 다른 성격의 질문을 드려서 죄송합니다..

이수홍

unread,
Dec 13, 2016, 9:15:11 PM12/13/16
to ks...@googlegroups.com
동일한 해시코드가 반환 될겁니다.

그 증거는 String Class 보시면 

직접 문자열에서 hashCode를 도출하는 식이 재정의 되어 있습니다.

소스는 이렇네요

public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;

for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
이 식이 있는것을 보니 프로세스와 관계 없이 문자열에 따라 해시코드가 같습니다.

이야기 하신 부분인 Object.hashCode를 이야기 하시는 부분이 맞죠? 

그러면 해시코드는 우리가 생각하는 레퍼런스 참조 주소하고 다릅니다.

2016. 12. 14. 오전 11:07, 심준보 <ai...@igaworks.com> 작성:

--
이 메일은 Google 그룹스 'Korea Spring User Group Q&A' 그룹에 가입한 분들에게 전송되는 메시지입니다.
이 그룹에서 탈퇴하고 더 이상 이메일을 받지 않으려면 ksug+uns...@googlegroups.com에 이메일을 보내세요.
https://groups.google.com/group/ksug에서 이 그룹을 방문하세요.
웹에서 이 토론을 보려면 https://groups.google.com/d/msgid/ksug/eca98781-e583-4f39-9bce-71d51071e89b%40googlegroups.com을(를) 방문하세요.
더 많은 옵션을 보려면 https://groups.google.com/d/optout을(를) 방문하세요.

심준보

unread,
Dec 13, 2016, 9:20:38 PM12/13/16
to Korea Spring User Group Q&A
코바님 정말 감사드립니다!

2016년 12월 14일 수요일 오전 11시 7분 44초 UTC+9, 심준보 님의 말:

Jin Kim

unread,
Dec 13, 2016, 9:45:37 PM12/13/16
to ks...@googlegroups.com
String 클래스의 구현 상세와 상관없이 hashCode 는 equals 로 비교할 때 같은 객체에 대해선 같은 값을 가져야 합니다.

Object.hashCode() 메서드에 대한 java api doc 의 설명은 다음과 같습니다.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.




2016년 12월 14일 오전 11:20, 심준보 <ai...@igaworks.com>님이 작성:

--
이 메일은 Google 그룹스 'Korea Spring User Group Q&A' 그룹에 가입한 분들에게 전송되는 메시지입니다.
이 그룹에서 탈퇴하고 더 이상 이메일을 받지 않으려면 ksug+unsubscribe@googlegroups.com에 이메일을 보내세요.

https://groups.google.com/group/ksug에서 이 그룹을 방문하세요.

심준보

unread,
Dec 13, 2016, 11:15:36 PM12/13/16
to Korea Spring User Group Q&A
김진님도 답변 감사합니다! 참고 하겠습니다.


2016년 12월 14일 수요일 오전 11시 7분 44초 UTC+9, 심준보 님의 말:
Reply all
Reply to author
Forward
0 new messages