ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ์•„์ดํ…œ14. Comparable์„ ๊ตฌํ˜„ํ• ์ง€ ๊ณ ๋ คํ•˜๋ผ
    DevBook/Effective Java 2023. 5. 7. 22:52

    ๐Ÿ“์ƒํ™ฉ

    public interface Comparable<T> {
        int compareTo(T t);
    }
    • Comparable ์ธํ„ฐํŽ˜์ด์Šค๋Š” compareTo() ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•จ
    • Comparable์„ ๊ตฌํ˜„ํ–ˆ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋“ค์—๋Š” ์ž์—ฐ์ ์ธ ์ˆœ์„œ๊ฐ€ ์žˆ์Œ์„ ๋œปํ•จ --> ํ•ด๋‹น ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•จ
    • ์•ŒํŒŒ๋ฒณ, ์ˆซ์ž, ์—ฐ๋Œ€ ๊ฐ™์ด ์ˆœ์„œ๊ฐ€ ๋ช…ํ™•ํ•œ ๊ฐ’ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์ž

     

    *compareTo ๋ฉ”์„œ๋“œ์˜ ์ผ๋ฐ˜ ๊ทœ์•ฝ

    • equals ๊ทœ์•ฝ๊ณผ ๋˜‘๊ฐ™์ด ๋ฐ˜์‚ฌ์„ฑ, ๋Œ€์นญ์„ฑ, ์ถ”์ด์„ฑ์„ ์ถฉ์กฑํ•ด์•ผ ํ•จ
      • ๋”ฐ๋ผ์„œ ๊ธฐ์กด ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•œ ๊ตฌ์ฒด ํด๋ž˜์Šค์—์„œ ์ƒˆ๋กœ์šด ๊ฐ’ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค๋ฉด compareTo ๊ทœ์•ฝ์„ ์ง€ํ‚ฌ ๋ฐฉ๋ฒ•์ด ์—†์Œ
      • ์šฐํšŒ๋ฒ•๋„ equals์™€ ๋™์ผํ•˜๊ฒŒ ํ™•์žฅํ•˜๋Š” ๋Œ€์‹  ๋…๋ฆฝ๋œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ์ด ํด๋ž˜์Šค์— ์›๋ž˜ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋จ

     

    ๐Ÿ“๋ฐฉ๋ฒ•

    • Comparable์€ ํƒ€์ž…์„ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š” ์ œ๋„ค๋ฆญ ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฏ€๋กœ compareTo ๋ฉ”์„œ๋“œ์˜ ์ธ์ˆ˜ ํƒ€์ž…์€ ์ปดํŒŒ์ผํƒ€์ž„์— ์ •ํ•ด์ง
      • ์ž…๋ ฅ ์ธ์ˆ˜์˜ ํƒ€์ž…์„ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ํ˜•๋ณ€ํ™˜ํ•  ํ•„์š”์—†์Œ
    • compareTo ๋ฉ”์„œ๋“œ๋Š” ์ˆœ์„œ๋ฅผ ๋น„๊ตํ•˜๋ฏ€๋กœ ๊ฐ์ฒด ์ฐธ์กฐ ํ•„๋“œ๋ฅผ ๋น„๊ตํ•˜๋ ค๋ฉด compareTo ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•จ
      • Comparable์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์€ ํ•„๋“œ๋‚˜ ํ‘œ์ค€์ด ์•„๋‹Œ ์ˆœ์„œ๋กœ ๋น„๊ตํ•ด์•ผ ํ•œ๋‹ค๋ฉด Comparator๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•จ --> ์ง์ ‘ ๋งŒ๋“ค๊ฑฐ๋‚˜ ์ž๋ฐ”๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ ์ค‘์— ์‚ฌ์šฉ

     

    ์ฝ”๋“œ 14-1 ๊ฐ์ฒด ์ฐธ์กฐ ํ•„๋“œ๊ฐ€ ํ•˜๋‚˜๋ฟ์ธ ๋น„๊ต์ž

    public final class CaseInsensitiveString implements Comparable<CaseInsensitiveString> {
    	
        public int compareTo(CaseInsensitiveString cis) {
        	return String.CASE_INSENSITIVE_ORDER.compare(s, cis.s);
        }
    }
    • ์ž๋ฐ” 7๋ถ€ํ„ฐ ๋ฐ•์‹ฑ๋œ ๊ธฐ๋ณธ ํƒ€์ž… ํด๋ž˜์Šค๋“ค์— ์ •์  ๋ฉ”์„œ๋“œ์ธ compare์„ ์ œ๊ณตํ•จ
    • compareTo ๋ฉ”์„œ๋“œ์—์„œ ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž < ์™€ > ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์ „ ๋ฐฉ์‹์€ ์˜ค๋ฅ˜๋ฅผ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ด์ œ๋Š” ์ถ”์ฒœํ•˜์ง€ ์•Š์Œ

     

    ๋น„๊ต์ž ์ƒ์„ฑ ๋ฉ”์„œ๋“œ(comparator construction method) ์‚ฌ์šฉ

    • ์ž๋ฐ” 8์—์„œ๋Š” Comparator ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋น„๊ต์ž ์ƒ์„ฑ ๋ฉ”์„œ๋“œ์™€ ํ•จ๊ป˜ ๋ฉ”์„œ๋“œ ์—ฐ์‡„ ๋ฐฉ์‹์œผ๋กœ ๋น„๊ต์ž๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Œ

     

    ์ฝ”๋“œ 14-3 ๋น„๊ต์ž ์ƒ์„ฑ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•œ ๋น„๊ต์ž

    private static final Comparator<PhoneNumber> COMPARATOR =
            comparingInt((PhoneNumber pn) -> pn.areaCode)
                .thenComparingInt(pn -> pn.prefix)
                .thenComparingInt(pn -> pn.lineNum);
                
    public int compareTo(PhoneNumber pn) {
    	return COMPARATOR.compare(this, pn);
    }
    • ํด๋ž˜์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ๋น„๊ต์ž ์ƒ์„ฑ ๋ฉ”์„œ๋“œ 2๊ฐœ๋ฅผ ์ด์šฉํ•ด ๋น„๊ต์ž๋ฅผ ์ƒ์„ฑํ•จ

     

    '๊ฐ’์˜ ์ฐจ'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌํ˜„ํ•œ ๋น„๊ต์ž

    ์ฝ”๋“œ 14-5 ์ •์  compare ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•œ ๋น„๊ต์ž

    static Comparator<Object> hashCodeOrder = new Comparator<>() {
        public int compare(Object o1, Object o2) {
        	return Integer.compare(o1.hashCode(), o2.hashCode());
        }
    };

    ์ฝ”๋“œ 14-6 ๋น„๊ต์ž ์ƒ์„ฑ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•œ ๋น„๊ต์ž

    static Comparator<Object> hashCodeOrder = Comparator.comparingInt(o -> o.hashCode());

     

    ๐Ÿ“ํ•ต์‹ฌ ์ •๋ฆฌ

    • ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋Š” ๊ฐ’ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ๊ผญ Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
    • compareTo ๋ฉ”์„œ๋“œ์—์„œ ํ•„๋“œ์˜ ๊ฐ’ ๋น„๊ตํ•  ๋•Œ < ์™€ > ์—ฐ์‚ฐ์ž๋Š” ์“ฐ์ง€ ๋ง์•„์•ผ ํ•จ
    • ๊ทธ ๋Œ€์‹  ๋ฐ•์‹ฑ๋œ ๊ธฐ๋ณธ ํƒ€์ž… ํด๋ž˜์Šค๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ •์  compare ๋ฉ”์„œ๋“œ๋‚˜ Comparator ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ œ๊ณตํ•˜๋Š” Comparator ์ƒ์„ฑ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ž

    ๋Œ“๊ธ€

Designed by Tistory.