더북(TheBook)

2.2.5 equals와 hashCode 메서드 만들기

Person 클래스가 데이터를 표현한다면 hashCodeequals 메서드도 필요하다. 자바에서 이런 메서드를 작성하는 일은 지겹고 실수하기도 쉬운 일이다. 다행히 좋은 자바 IDE는 여러분 대신 이런 메서드를 자동으로 생성해 준다. 다음 예제는 인텔리J IDEA가 제공하는 이런 기능으로 만든 코드다.

예제 2-2 인텔리J IDEA가 생성한 자바 데이터 객체

public final class Person {
    private final String name;
    private final Instant registered;

<span class="sgc-1">public</span> <span class="sgc-3">Person(String</span> <span class="sgc-3">name,</span> <span class="sgc-3">Instant</span> <span class="sgc-3">registered)</span> <span class="sgc-3">{</span>
    <span class="sgc-1">this</span><span class="sgc-3">.name</span> <span class="sgc-3">=</span> <span class="sgc-3">name;</span>
    <span class="sgc-1">this</span><span class="sgc-3">.registered</span> <span class="sgc-3">=</span> <span class="sgc-3">registered;</span>
<span class="sgc-3">}</span>

<span class="sgc-1">public</span> <span class="sgc-3">Person(String</span> <span class="sgc-3">name)</span> <span class="sgc-3">{</span>
    <span class="sgc-1">this</span><span class="sgc-3">(name,</span> <span class="sgc-3">Instant.now());</span>
<span class="sgc-3">}</span>

<span class="sgc-1">public</span> <span class="sgc-3">String</span> <span class="sgc-3">getName()</span> <span class="sgc-3">{</span>
    <span class="sgc-1">return</span> <span class="sgc-3">name;</span>
<span class="sgc-3">}</span>

<span class="sgc-1">public</span> <span class="sgc-3">Instant</span> <span class="sgc-3">getRegistered()</span> <span class="sgc-3">{</span>
    <span class="sgc-1">return</span> <span class="sgc-3">registered;</span>
<span class="sgc-3">}</span>

<span class="sgc-3">@Override</span>
<span class="sgc-1">public</span> <span class="sgc-3">boolean</span> <span class="sgc-3">equals(Object</span> <span class="sgc-3">o)</span> <span class="sgc-3">{</span>
    <span class="sgc-1">if</span> <span class="sgc-3">(</span><span class="sgc-1">this</span> <span class="sgc-3">==</span> <span class="sgc-3">o)</span> <span class="sgc-1">return</span> <span class="sgc-1">true</span><span class="sgc-3">;</span>
    <span class="sgc-1">if</span> <span class="sgc-3">(o</span> <span class="sgc-3">==</span> <span class="sgc-1">null</span> <span class="sgc-3">||</span> <span class="sgc-3">getClass()</span> <span class="sgc-3">!=</span> <span class="sgc-3">o.getClass())</span> <span class="sgc-1">return</span> <span class="sgc-1">false</span><span class="sgc-3">;</span>
    <span class="sgc-3">Person</span> <span class="sgc-3">person</span> <span class="sgc-3">=</span> <span class="sgc-3">(Person)</span> <span class="sgc-3">o;</span>
    <span class="sgc-1">return</span> <span class="sgc-3">Objects.equals(name,</span> <span class="sgc-3">person.name)</span> <span class="sgc-3">&amp;&amp;</span> <span class="sgc-3">Objects.equals(registered,</span> <span class="sgc-3">person.registered);</span>
<span class="sgc-3">}</span>

<span class="sgc-3">@Override</span>
<span class="sgc-1">public</span> <span class="sgc-3">int</span> <span class="sgc-3">hashCode()</span> <span class="sgc-3">{</span>
    <span class="sgc-1">return</span> <span class="sgc-3">Objects.hash(name,</span> <span class="sgc-3">registered);</span>
<span class="sgc-3">}</span>

}

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.