๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Java] hashCode() ์˜ค๋ฒ„๋ผ์ด๋“œ ์‹œ equals()๋„ ํ•จ๊ป˜ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์•ผํ•˜๋Š” ์ด์œ 

์ถœ์ฒ˜

ChatGPT


hashCode()์™€ equals() ๋ฉ”์„œ๋“œ๋ฅผ ํ•จ๊ป˜ ์žฌ์ •์˜ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” ์ž๋ฐ”์—์„œ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•˜๊ณ  ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜์—์„œ ์ •ํ™•ํ•˜๊ฒŒ ์ž‘๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋‹ค. ์ด ๋‘ ๋ฉ”์„œ๋“œ๋Š” ์„œ๋กœ ๋ฐ€์ ‘ํ•˜๊ฒŒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์œ ๋กœ ํ•จ๊ป˜ ์žฌ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.

 

1. Hash-Based Collections์˜ ์˜ฌ๋ฐ”๋ฅธ ๋™์ž‘

์ž๋ฐ”์˜ ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜(HashMap, HashSet, Hashtable ๋“ฑ)์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ์ฒด์˜ hashCode() ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ hashCode()์™€ equals() ๋ฉ”์„œ๋“œ์˜ ๊ณ„์•ฝ์„ ์ง€ํ‚ค๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

 

  • hashCode():
    • ๊ฐ์ฒด์˜ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํ•ด์‹œ ๊ธฐ๋ฐ˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์—์„œ ๊ฐ์ฒด๋ฅผ ๋ถ„๋ฅ˜ํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.
  • equals():
    • ๋‘ ๊ฐ์ฒด๊ฐ€ ๋™๋“ฑํ•œ์ง€๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์ฒด์˜ ๋…ผ๋ฆฌ์  ๋™๋“ฑ์„ฑ์„ ํŒ๋‹จํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

๋…ผ๋ฆฌ์  ๋™๋“ฑ์„ฑ ํŒ๋‹จ์„ ์ œ๋Œ€๋กœ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” hashCode()์™€ equals()๊ฐ€ ๊ฐ™์ด ์˜ค๋ฒ„๋ผ์ด๋“œ ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฑฐ๊ตฌ๋‚˜.

2. hashCode()์™€ equals()์˜ ๊ณ„์•ฝ

hashCode()์™€ equals()๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณ„์•ฝ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

 

1. ๊ฐ™์€ ๊ฐ์ฒด๋Š” ๊ฐ™์€ ํ•ด์‹œ ์ฝ”๋“œ

  • equals()๊ฐ€ ๋‘ ๊ฐ์ฒด๋ฅผ ๋™์ผํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜๋ฉด, hashCode()๋Š” ๋‘ ๊ฐ์ฒด์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, a.equals(b)๊ฐ€ true์ธ ๊ฒฝ์šฐ, a.hashCode()์™€ b.hashCode()๋„ ๋ฐ˜๋“œ์‹œ ๊ฐ™์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.

2. ๋‹ค๋ฅธ ๊ฐ์ฒด๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํ•ด์‹œ ์ฝ”๋“œ์ผ ํ•„์š” ์—†์Œ

  • equals()๊ฐ€ ๋‘ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฅด๋‹ค๊ณ  ํŒ๋‹จํ•˜๋”๋ผ๋„, hashCode()๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ํ•„์š”๋Š” ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ๋™์ผํ•œ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

3. ํ•ด์‹œ ์ฝ”๋“œ์˜ ์ผ๊ด€์„ฑ

  • ๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ํ•œ, ๊ฐ™์€ ๊ฐ์ฒด์— ๋Œ€ํ•œ hashCode()์˜ ๋ฐ˜ํ™˜ ๊ฐ’์€ ์ผ๊ด€๋˜๊ฒŒ ์œ ์ง€๋˜์–ด์•ผ ํ•œ๋‹ค.

 

3. ์‹ค์ œ ์˜ˆ์ œ

 

๋‹ค์Œ์€ hashCode()์™€ equals()๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์žฌ์ •์˜ํ•œ ์˜ˆ์ œ์ด๋‹ค.

import java.util.Objects;

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Person person = (Person) obj;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

 

 

  • equals() ๋ฉ”์„œ๋“œ๋Š” name๊ณผ age๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‘ Person ๊ฐ์ฒด์˜ ๋™๋“ฑ์„ฑ์„ ํŒ๋‹จํ•œ๋‹ค.
  • hashCode() ๋ฉ”์„œ๋“œ๋Š” name๊ณผ age๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • equals()๊ฐ€ ๋‘ ๊ฐ์ฒด๋ฅผ ๊ฐ™๋‹ค๊ณ  ํŒ๋‹จํ•˜๋ฉด, hashCode()๋Š” ๋‘ ๊ฐ์ฒด์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

4. ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜์˜ ์˜ˆ

ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜์—์„œ hashCode()์™€ equals()์˜ ์žฌ์ •์˜๊ฐ€ ์ค‘์š”ํ•œ ์ด์œ ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์ œ์ด๋‹ค.

import java.util.HashSet;

public class HashSetExample {
    public static void main(String[] args) {
        HashSet<Person> set = new HashSet<>();
        Person p1 = new Person("Alice", 30);
        Person p2 = new Person("Alice", 30);

        set.add(p1);
        System.out.println(set.contains(p2)); // true
    }
}

 

  • p1๊ณผ p2๋Š” name๊ณผ age๊ฐ€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— equals()๊ฐ€ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • HashSet์—์„œ contains() ๋ฉ”์„œ๋“œ๋Š” hashCode()์™€ equals()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค. ๋‘ ๊ฐ์ฒด๊ฐ€ ๋™๋“ฑํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด, contains()๋Š” true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

 

 


ChatGPT์˜ ์ถœ

์ฐธ๊ณ  ์ž๋ฃŒ

  • Oracle Java Documentation - Object.hashCode() Method
  • Effective Java by Joshua Bloch - ์ด ์ฑ…์€ hashCode()์™€ equals()์˜ ๊ณ„์•ฝ๊ณผ ์žฌ์ •์˜์— ๋Œ€ํ•œ ๊ถŒ์žฅ ์‚ฌํ•ญ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.