[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item14. Comparable์„ ๊ตฌํ˜„ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋ผ. (Comparable ๊ตฌํ˜„ ๋ฐฉ๋ฒ• #1)

2023. 2. 6. 13:48ใ†JAVA/Effective JAVA

728x90

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item14. Comparable์„ ๊ตฌํ˜„ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋ผ.

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item14. Comparable์„ ๊ตฌํ˜„ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋ผ. (compareTo ๊ธฐ๋ณธ ๊ทœ์•ฝ) (tistory.com)

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item14. Comparable์„ ๊ตฌํ˜„ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋ผ. (compareTo ๊ธฐ๋ณธ ๊ทœ์•ฝ)

item14. Comparable์„ ๊ตฌํ˜„ํ• ์ง€ ๊ณ ๋ฏผํ•˜๋ผ. compareTo : compareTo๋Š” Comparable ์ธํ„ฐํŽ˜์ด์Šค์˜ ์œ ์ผํ•œ ๋ฉ”์„œ๋“œ๋กœ Objet.equals(๋‹จ์ˆœ ๋™์น˜์„ฑ)์— ๋”ํ•ด์„œ ์ˆœ์„œ๊นŒ์ง€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ Generic์„ ์ง€์›ํ•œ๋‹ค. Comparable์„ ๊ตฌํ˜„ํ–ˆ

hyejin.tistory.com

์—ฌ๊ธฐ์„œ ์šฐ์„  CompareTo์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ๊ทœ์•ฝ์— ๋Œ€ํ•ด์„œ ๋ดค๊ณ , ์ด์ œ compareTo๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

 

 

public class PhoneNumber implements Comparable<PhoneNumber>
{
  ...
}

๋จผ์ € ์ž์—ฐ์ ์ธ ์ˆœ์„œ natural order๋ฅผ ์ œ๊ณตํ•  ํด๋ž˜์Šค์— implements Comparable<T>๋ฅผ ์„ ์–ธํ•œ๋‹ค. 

Comparable์€ ํƒ€์ž…์„ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š” ์ œ๋„ค๋ฆญ ์ธํ„ฐํŽ˜์ด์Šค์ด๋ฏ€๋กœ compareTo ๋ฉ”์„œ๋“œ์— ์ธ์ˆ˜ ํƒ€์ž…์€ ์ปดํŒŒ์ผ ํƒ€์ž„์— ์ •ํ•ด์ง„๋‹ค. (์ž…๋ ฅ ์ธ์ˆ˜์˜ ํƒ€์ž…์„ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ํ˜•๋ณ€ํ™˜ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๋œป) 

 

 

@Override
public int compareTo(PhoneNumber o)
{
   int result = Short.compare(areaCode, o.areaCode);
   if (result == 0) {
      result = Short.compare(prefix, o.prefix);
      
      if (result == 0) {
         result = Short.compare(lineNum, o.lineNum);
      }
   }
   return result;
}

๊ทธ๋ฆฌ๊ณ  compareTo ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค. 

compareTo ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ๊ธฐ๋ณธ ํƒ€์ž…์€ ๋ฐ•์‹ฑ๋œ ๊ธฐ๋ณธ ํƒ€์ž… compare์„ ์‚ฌ์šฉํ•ด ๋น„๊ตํ•œ๋‹ค. (Short.compare)

 

๊ทธ๋ฆฌ๊ณ  ์ด์ œ ๋น„๊ตํ•  ๋•Œ ํ•ต์‹ฌ ํ•„๋“œ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋ผ๋ฉด ๋น„๊ต ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. 

์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ•„๋“œ๋ฅผ ๋น„๊ตํ•˜๊ณ  ๊ทธ ๊ฐ’์ด 0์ด๋ผ๋ฉด ๋‹ค์Œ ํ•„๋“œ๋ฅผ ๋น„๊ตํ•˜๋Š” ์ˆœ์„œ๋กœ ์ด์–ด๊ฐ„๋‹ค. 

 

 

๊ทผ๋ฐ ์ด์ œ ๊ธฐ์กด ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•˜๊ณ  ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ์—” compareTo ๊ทœ์•ฝ์„ ์ง€ํ‚ฌ ์ˆ˜ ์—†๋‹ค. 

public class Point implements Comparable<Point>
{
   final int x, y;
   
   public Point(int x, int y)
   {
      this.x = x;
      this.y = y;
   }
   
   public int getX()
   {
      return x;
   }
   
   public int getY()
   {
      return y;
   }
   
   @Override
   public int compareTo(Point o)
   {
      int result = Integer.compare(this.x, o.x);
      if (result == 0) {
         result = Integer.compare(this.y, o.y);
      }
      
      return result;
   }
}

์ด๋ ‡๊ฒŒ ์ƒ์œ„ ํด๋ž˜์Šค์—์„œ Comparable์„ implementsํ•˜๊ณ  compareTo ๊ตฌํ˜„ํ–ˆ๋‹ค. 

 

 

 

public class NamedPoint extends Point
{
   final private String name;
   
   public NamedPoint(int x, int y, String name)
   {
      super(x, y);
      this.name = name;
   }
}

๊ทธ๋ฆฌ๊ณ  ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ Point ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์ถ”๊ฐ€๋กœ name ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๋น„๊ต๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•œ๋‹ค.. 

๊ทธ๋Ÿฌ๋ฉด ๋‹ค์‹œ Comparable์„ ๊ตฌํ˜„ํ•˜๊ณ  ํ•˜๊ณ  ์‹ถ๊ฒ ์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์—†๋‹ค. 

 

public static void main(String[] args)
{
   NamedPoint p1 = new NamedPoint(1, 0, "hyejin");
   NamedPoint p2 = new NamedPoint(1, 0, "chamo");
   
   Set<NamedPoint> points = new TreeSet<>(new Comparator<NamedPoint>()
   {
      @Override
      public int compare(NamedPoint p1, NamedPoint p2)
      {
         int result = Integer.compare(p1.getX(), p2.getX());
         if (result == 0) {
            result = Integer.compare(p1.getY(), p2.getY());
         }
         if (result == 0) {
            result = p1.name.compareTo(p2.name);
         }
         
         return result;
      }
   });
   
   points.add(p1);
   points.add(p2);
   
   System.out.println("points = " + points);
}

๋Œ€์‹  ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ์ž์— Comparator๋ฅผ ์ „๋‹ฌํ•ด์„œ compare๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ธด ํ•œ๋ฐ ๋น„์ถ”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

-> Composition์„ ์‚ฌ์šฉํ•˜์ž!! 

 

public class Point implements Comparable<Point>
{
   
   final int x, y;
   
   public Point(int x, int y)
   {
      this.x = x;
      this.y = y;
   }
   
   @Override
   public int compareTo(Point o)
   {
      int result = Integer.compare(this.x, o.x);
      if (result == 0) {
         result = Integer.compare(this.y, y);
      }
      return result;
   }
}
public class NamedPoint implements Comparable<NamedPoint>
{
   private final Point point;
   
   private final String name;
   
   public NamedPoint(Point point, String name)
   {
      this.point = point;
      this.name = name;
   }
   
   public Point getPoint()
   {
      return this.getPoint();
   }
   @Override
   public int compareTo(NamedPoint o)
   {
      int result = this.point.compareTo(o.point);
      if (result == 0) {
         result = this.name.compareTo(o.name);
      }
      return result;
   }
}

-> Comparable์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•˜๊ณ  ๊ฐ’ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ํ™•์žฅํ•˜๋Š” ๋Œ€์‹  ๋…๋ฆฝ์ ์ธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์ด ํด๋ž˜์Šค์— ์›๋ž˜ ํด๋ž˜์Šค ์ด๋Šํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•„๋“œ๋ฅผ ๋‘๊ณ , ๊ทธ ๋‹ค์Œ ๋‚ด๋ถ€ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” '๋ทฐ' ๋ฉ”์„œ๋“œ(getPoint)๋ฅผ ์ œ๊ณตํ•˜๋ฉด ๋œ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90