JAVA & OOP

[JAVA] HashCode์™€ equals ์˜ค๋ฒ„๋ผ์ด๋”ฉ

DAHLIA CHOI 2024. 3. 21. 13:36

 

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์œ„์น˜ Id๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๋กœ์ง์ด ์žˆ์–ด์„œ hashCode๋ž‘ equals๋ฅผ ์žฌ์ •์˜ํ–ˆ๋‹ค.!

๊ทผ๋ฐ ๋‚ด๊ฐ€ ์ด ๋‘ ๊ฐœ๋ฅผ ์ž์„ธํžˆ ๊ณต๋ถ€ํ•ด ๋ณธ ์ ์ด ์žˆ์—ˆ๋‚˜ ์‹ถ์–ด์„œ ์ด์ฐธ์— ์ •๋ฆฌํ•ด์„œ ์ดํ•ดํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค!

 

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        GetAllPlaceResDto that = (GetAllPlaceResDto) o;
        return Objects.equals(placeId, that.placeId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(placeId);
    }
ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ !

 

 

 

๐ŸŒฑ equals ๋ฉ”์„œ๋“œ๋ž€?

์–ด๋–ค ๋‘ ์ฐธ์กฐ ๋ณ€์ˆ˜ ๊ฐ’์ด ๊ฐ™์€์ง€ ๋‹ค๋ฅธ์ง€ ๋™๋“ฑ ์—ฌ๋ถ€๋ฅผ ๋น„๊ตํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด equals() ๋ฉ”์„œ๋“œ์ด๋‹ค. 

 

์ž๋ฐ”์—์„œ ๋ณ€์ˆ˜๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์ด ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ equals(๋™๋“ฑ์„ฑ)์™€ ==(๋™์ผ์„ฑ)์ด๋‹ค.

๋™๋“ฑ์„ฑ : ๊ฐ์ฒด๊ฐ€ ์ฃผ์†Œ๊ฐ’์ด ๋‹ค๋ฅด๋”๋ผ๋„ ๋‚ด์šฉ์ด ๊ฐ™๋‹ค๋ฉด ๊ฐ™๋‹ค๊ณ  ๋ณด๋Š” ๊ฒƒ

๋™์ผ์„ฑ : ๊ฐ์ฒด๊ฐ€ ์ฃผ์†Œ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด ์•„๋ฌด๋ฆฌ ๋‹ค๋ฅธ ๋‚ด์šฉ์ด๋ผ๋„ ๊ฐ™์ง€ ์•Š๋‹ค๊ณ  ๋ณด๋Š” ๊ฒƒ

 

String s1 = "a";
String s2 = "a";

System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true

์ด์ฒ˜๋Ÿผ ๋ณ€์ˆ˜๋ฅผ ๋น„๊ตํ•  ๋•Œ๋Š” ๋‘ ๋ฐฉ์‹์ด ๋‹ค๋ฅธ ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

ํ•˜์ง€๋งŒ, ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” equals()์™€ ==์ด ๊ฐ™์€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

 

๐Ÿค” ์™œ ๊ทธ๋Ÿด๊นŒ?

์ž๋ฐ” ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์ธ Object ํด๋ž˜์Šค์— ์žˆ๋Š” equals()๋Š” ๋น„๊ต์—ฐ์‚ฐ์ž์ธ ==์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

์ฆ‰, ์›๋ž˜์˜ equals๋Š” ์ฐธ์กฐ๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

public boolean equals(Object obj) {
    return (this == obj);
}

 

 

์ฆ‰, String ํด๋ž˜์Šค์—์„œ๋Š” equals ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๋ง์ด๋‹ค.

    // String ํด๋ž˜์Šค์˜ equals ๋ฉ”์„œ๋“œ
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        return (anObject instanceof String aString)
                && (!COMPACT_STRINGS || this.coder == aString.coder)
                && StringLatin1.equals(value, aString.value);
    }

 

 

 

๊ทธ๋Ÿผ ์ด์ œ equals()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด๋ณด์ž

Member ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ…Œ์ŠคํŠธํ•ด๋ณด์•˜๋‹ค!

public class Member {

    private String name;

    public Member(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true; // ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์ฐธ์กฐ๊ฐ’์ด ๊ฐ™์€๊ฒฝ์šฐ true๋ฅผ ๋ฐ”๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.
        if (o == null || getClass() != o.getClass()) return false; //๋น„๊ตํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ null์ธ์ง€ ํด๋ž˜์Šค๊ฐ€ ๊ฐ™์€์ง€ ์ฒดํฌํ•œ๋‹ค.
        Member member = (Member) o;
        return Objects.equals(name, member.name); //๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๊ฐ’๋“ค์„ ๋น„๊ตํ•˜์—ฌ ๋ฆฌํ„ดํ•œ๋‹ค.
    }

}

 

        Member member1 = new Member("name");
        Member member2 = new Member("name");

        System.out.println("member 1 == member2 : " + member1.equals(member2));
        System.out.println("member1 hashcode : " + member1.hashCode());
        System.out.println("member2 hashcode : " + member2.hashCode());

 

 

๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด member1๊ณผ member2์˜ ๊ฐ์ฒด๊ฐ€ ๊ฐ™๋‹ค๋Š” ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

 

ํ•˜์ง€๋งŒ hashcode๋Š” ๋™์ผํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

๋”ฐ๋ผ์„œ equals๋ฅผ ์žฌ์ •์˜ ํ•œ๋‹ค๋ฉด hashcode ๋˜ํ•œ ์žฌ์ •์˜ ํ•ด์ค˜์•ผ ํ•œ๋‹ค!

 

 

๐ŸŒฑ HashCode๋ž€?

๊ฐ์ฒด๋ฅผ ์‹๋ณ„ํ•˜๋Š” ํ•˜๋‚˜์˜ ๊ณ ์œ  ์ •์ˆ˜๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.

์ฆ‰, ๊ฐ์ฒด์˜ ์ฃผ์†Œ ๊ฐ’์„ ์ด์šฉํ•ด์„œ ํ•ด์‹ฑ ๊ธฐ๋ฒ•์„ ํ†ตํ•ด ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด๋Š” ๊ฐ™์€ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.

 

์ž๋ฐ”์—์„œ ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ (HashSet, HashMap, HashTable)์—์„œ๋„ hashCode๋ฅผ ์ด์šฉํ•ด์„œ ๋™๋“ฑ์„ฑ ๋น„๊ต๋ฅผ ํ•˜๋Š”๋ฐ ์ด๋•Œ hashCode๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์ธ์‹ํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค!

 

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

๋”ฐ๋ผ์„œ ๋‹ค์‹œ Member ๊ฐ์ฒด์— ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜๊ณ  ๋‚˜์„œ ์‹คํ–‰ํ•ด ๋ณด๋‹ˆ, ๊ฐ™์€ ํ•ด์‹œ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

 

 

์ถ”๊ฐ€์ ์œผ๋กœ ๋‚ด ํ”„๋กœ์ ํŠธ์—์„œ๋„ HashSet์„ ์ด์šฉํ•ด์„œ ์œ„์น˜ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— hashCode๋ฅผ ์žฌ์ •์˜ ํ•œ ๊ฒƒ์ด๋‹ค!

 Set<GetAllPlaceResDto> getAllPlaceResDtoList = new HashSet<>();

 

์—ฌ๊ธฐ์—์„œ๋Š” DTO์•ˆ์— equals๋ž‘ hashcode๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ–ˆ๋‹ค!!