CORE
HOME > JAVA > J2SE > CORE
2019.05.29 / 24:52

[Á¤·Ä] Comparable°ú Comparator

Źµ¹ÀÌ°³¹ßÀÚ
Ãßõ ¼ö 312

ÀÚ¹Ù¿¡¼­ Á¤·ÄÀ» ÇÏ´Ù º¸¸é Comparable°ú Comparator¸¦ ÀÚÁÖ ¸¶ÁÖÄ¡°Ô µÈ´Ù. º» °Ô½Ã±Û¿¡¼­´Â Comparable°ú ComparatorÀÌ ¹«¾ùÀÌ¸ç ¾î¶»°Ô ´Ù¸¥Áö¸¦ ¸ÕÀú »ìÆ캸°í, °¢°¢ÀÇ »ç¿ë¹ýÀ» ¿¹Á¦¸¦ ÅëÇØ ¾Ë¾Æº¸µµ·Ï ÇÏ°Ú´Ù.

1. Comparable°ú Comparator¶õ?
  1) Comparable ÀÎÅÍÆäÀ̽º
    - Á¤ÀÇ: Á¤·Ä ¼öÇà½Ã ±âº»ÀûÀ¸·Î Àû¿ëµÇ´Â Á¤·Ä ±âÁØÀÌ µÇ´Â ¸Þ¼­µå¸¦ Á¤ÀÇÇØ ³õ´Â ÀÎÅÍÆäÀ̽ºÀÌ´Ù.
    - »ç¿ë¹ý: Comparable ÀÎÅÍÆäÀ̽º¸¦ implements ÇÑ µÚ, ³»ºÎ¿¡ Àִ compareTo ¸Þ¼­µå¸¦ ¿øÇÏ´Â Á¤·Ä ±âÁØ´ë·Î ±¸ÇöÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ´Ù.
    - ÆÐÅ°Áö: java.lang.Comparable
    - ÀÚ¹Ù¿¡¼­ Á¦°øµÇ´Â Á¤·ÄÀÌ °¡´ÉÇÑ Å¬·¡½ºµéÀº ¸ðµÎ Comparable ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÏ°í ÀÖÀ¸¸ç, Á¤·Ä½Ã¿¡ ComparableÀÇ ±¸Çö ³»¿ë¿¡ ¸ÂÃç Á¤·ÄÀÌ ¼öÇàµÈ´Ù. 
¿¹¸¦µé¾î Integer, Double µîÀÇ Å¬·¡½ºÀÇ °æ¿ì ºñ ³»¸²Â÷¼ø(¿À¸§Â÷¼ø°ú À¯»ç), String Ŭ·¡½ºÀÇ °æ¿ì »çÀü¼øÀ¸·Î Á¤·ÄµÇ°Ô ±¸ÇöµÇ¾î ÀÖ´Ù.

  2) Comparator Ŭ·¡½º
    - Á¤ÀÇ: Á¤·Ä °¡´ÉÇÑ Å¬·¡½º(=ComparableÀÌ ±¸ÇöµÈ Ŭ·¡½º)µéÀÇ ±âº» Á¤·Ä ±âÁØ°ú´Â ´Ù¸¥ ¹æ½ÄÀ¸·Î Á¤·ÄÇÏ°í ½ÍÀ» ¶§ »ç¿ëÇϴ Ŭ·¡½ºÀÌ´Ù.
    - »ç¿ë¹ý: Comparator Ŭ·¡½º¸¦ »ý¼ºÇÏ¿©³»ºÎ¿¡ compare¸Þ¼­µå¸¦ ¿øÇÏ´Â Á¤·Ä ±âÁØ´ë·Î ±¸ÇöÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ´Ù.
    - ÆÐÅ°Áö: java.util.Comparator
    - ÁÖ·Î À͸íŬ·¡½º(new Comparator(){ ... })·Î »ç¿ëµÇ¸ç, ±âº»ÀûÀ¸·Î ¿À¸§Â÷¼øÀÌ Á¤·Ä ±âÁØÀÎ °ÍÀ» ³»¸²Â÷¼øÀ¸·Î Á¤·ÄÇÏ´Â µîÀÇ ¿ëµµ·Î »ç¿ëµÈ´Ù.



2. »ç¿ë ¹æ¹ý
  1) Comparable 

¾Æ·¡¿Í °°ÀÌ ´ëÇлý Ŭ·¡½º°¡ ÀÖ´Ù°í ÇÏÀÚ. ÀÌ ¶§, 5¸íÀÇ ´ëÇлýµéÀÇ Á¤º¸¸¦ °ü¸®Çϱâ À§ÇØ ±æÀÌ 5Â¥¸®ÀÇ ´ëÇлý Ŭ·¡½º ÇüÅ ¹è¿­À» »ý¼ºÇÏ¿´°í, Çйø¼øÀ¸·Î Á¤·ÄÇؼ­ ³ªÅ¸³»°íÀÚ ÇÑ´Ù. ¾î¶»°Ô Çϸé ÁÁÀ»±î?

class Student{ String name; //À̸§ int id; //Çйø double score; //ÇÐÁ¡ public Student(String name, int id, double score){ this.name = name; this.id = id; this.score = score; } public String toString(){ //Ãâ·Â¿ë toString¿À¹ö¶óÀ̵å return "À̸§: "+name+", Çйø: "+id+", ÇÐÁ¡: "+score; } } public class Main{ public static void main(String[] args) { Student student[] = new Student[5]; //¼ø¼­´ë·Î "À̸§", Çйø, ÇÐÁ¡ student[0] = new Student("Dave", 20120001, 4.2); student[1] = new Student("Amie", 20150001, 4.5); student[2] = new Student("Emma", 20110001, 3.5); student[3] = new Student("Brad", 20130001, 2.8); student[4] = new Student("Cara", 20140001, 4.2); } }

Student Ŭ·¡½º¿¡ ±âº» Á¤·Ä ±âÁØÀÌ ¾øÀ¸¹Ç·Î ¿ì¼± java.lang.Comparable ÆÐÅ°Áö¸¦ import ÇÑ ´ÙÀ½ ComparableÀ» implements ÇØÁØ´Ù. 
ÀÌ ¶§, ÁÖÀÇÇØ¾ß ÇÒ °ÍÀº ComparableÀÇ Á¦³Ê¸¯½ºÀε¥, ¿ì¸®´Â Student Ŭ·¡½º ³¢¸® ºñ±³ÇÏ´Â °ÍÀ̹ǷΠÁ¦³Ê¸¯½º ŸÀÔµµ Student°¡ µÈ´Ù.
ComparableÀ» implementsÇÏ°í ³ª¸é ±¸ÇöÇÏÁö ¾ÊÀº ¸Þ¼­µå¸¦ ±¸ÇöÇ϶ó°í ³ª¿À´Âµ¥, ÀÌ ¸Þ¼­µå´Â compareTo¸Þ¼­µå·Î °æ°í ¸Þ¼¼Áö¸¦ Ŭ¸¯Çϰųª Á÷Á¢ ¿À¹ö¶óÀ̵ù ÇØ ÁÖ¸é µÈ´Ù.

unimplemented methods = compareTo()
import java.lang.Comparable; //ÆÐÅ°Áö import class Student implements Comparable<Student> { //Á¦³Ê¸¯½º ÁÖÀÇ! String name; //À̸§ int id; //Çйø double score; //ÇÐÁ¡ public Student(String name, int id, double score){ this.name = name; this.id = id; this.score = score; } public String toString(){ //Ãâ·Â¿ë toString¿À¹ö¶óÀ̵å return "À̸§: "+name+", Çйø: "+id+", ÇÐÁ¡: "+score; } @Override public int compareTo(Student anotherStudent) { //¿À¹ö¶óÀ̵ù // TODO Auto-generated method stub return 0; } }


ÀÌÁ¦ compareTo ¸Þ¼­µå ³»¿ëÀ» ¿ì¸®°¡ ¿øÇϴ´ë·Î ±¸ÇöÇØ¾ß ÇÑ´Ù. ¿ì¸®°¡ ¿øÇÏ´Â Á¤·Ä ±âÁØÀº 'Çйø ¿À¸§Â÷¼ø'À̹ǷΠ¾Æ·¡¿Í °°ÀÌ ±¸ÇöÇØÁÖ¸é µÈ´Ù.

@Override public int compareTo(Student anotherStudent) { // TODO Auto-generated method stub return Integer.compare(id, anotherStudent.id); }

Integer.compareÀº ´Ü¼øÈ÷ ù¹ø° ¸Å°³º¯¼ö¿Í µÎ¹ø° ¸Å°³º¯¼ö°¡ ¿À¸§Â÷¼øÀ¸·Î À¯ÁöµÉ ¼ö ÀÖµµ·Ï °ªÀ» ºñ±³ÇØ ÁÖ´Â ¸Þ¼­µåÀÌ´Ù. IntegerŬ·¡½º¿¡¼­ ÇÔ¼ö ¿øÇüÀ» ã¾Æº¸¸é ¾Æ·¡¿Í °°´Ù.

IntegerŬ·¡½º ³»ºÎ¿¡ Á¤ÀÇµÈ compare ¸Þ¼­µå

ÀÌ int compare ¸Þ¼­µå¸¦ °£´ÜÈ÷ ¼³¸íÇÏÀÚ¸é, Á¤·ÄÀÌ ÁøÇàµÉ ¶§ ÀÚ¸®¹Ù²Þ(=Á¤·Ä) ¿©ºÎ¸¦ °áÁ¤ÇÏ´Â °ªÀ» ³Ñ°ÜÁÖ´Â ¿ªÇÒÀ» ÇÑ´Ù.
¸¸¾à return°ªÀÌ 0À̳ª À½¼öÀ̸é ÀÚ¸®¹Ù²ÞÀ» ÇÏÁö ¾Ê°í¾ç¼öÀ̸é ÀÚ¸®¹Ù²ÞÀ» ¼öÇàÇÑ´Ù.
¸¸¾à ¿À¸§Â÷¼øÀÌ ¾Æ´Ï¶ó ³»¸²Â÷¼øÀ¸·Î Á¤·ÄÇÏ°í ½Í´Ù¸é ¸Å°³º¯¼öÀÇ ¼ø¼­¸¦ ¹Ù²ãÁÖ¸é µÈ´Ù.

µû¶ó¼­ Integer.compare¸¦ »ç¿ëÇÏÁö ¾Ê°í ¾Æ·¡¿Í °°ÀÌ Á÷Á¢ ±¸ÇöÇصµ »ó°ü ¾ø´Ù.

@Override public int compareTo(Student anotherStudent) { // TODO Auto-generated method stub return (id<anotherStudent.id)?-1:((id==anotherStudent.id)?0:1); }


¿©±â±îÁö ÇßÀ¸¸é ±âº» Á¤·Ä ±âÁØÀÌ ¿Ï¼ºµÈ °ÍÀÌ´Ù. À̸¦ ¹ÙÅÁÀ¸·Î main() ¸Þ¼­µå¿¡¼­ ÀÚ¹Ù¿¡¼­ ±âº»ÀûÀ¸·Î Á¦°øÇÏ´Â Äü¼ÒÆ® ¸Þ¼­µåÀÎ Arrays.sort()¸¦ ÀÌ¿ëÇÏ¿© Á¤·ÄÀ» ¼öÇàÇغ¸¸é Çйø ¿À¸§Â÷¼øÀ¸·Î Àß Á¤·ÄµÈ °ÍÀ» È®ÀÎÇÒ ¼ö ÀÖ´Ù.

import java.lang.Comparable; import java.util.Arrays; //Äü¼ÒÆ® »ç¿ëÇϱâ À§ÇØ import class Student implements Comparable<Student> { String name; //À̸§ int id; //Çйø double score; //ÇÐÁ¡ public Student(String name, int id, double score){ this.name = name; this.id = id; this.score = score; } public String toString(){ //Ãâ·Â¿ë toString¿À¹ö¶óÀ̵å return "À̸§: "+name+", Çйø: "+id+", ÇÐÁ¡: "+score; } /* ±âº» Á¤·Ä ±âÁØ: Çйø ¿À¸§Â÷¼ø */ public int compareTo(Student anotherStudent) { return Integer.compare(id, anotherStudent.id); } } public class Main{ public static void main(String[] args) { Student student[] = new Student[5]; //¼ø¼­´ë·Î "À̸§", Çйø, ÇÐÁ¡ student[0] = new Student("Dave", 20120001, 4.2); student[1] = new Student("Amie", 20150001, 4.5); student[2] = new Student("Emma", 20110001, 3.5); student[3] = new Student("Brad", 20130001, 2.8); student[4] = new Student("Cara", 20140001, 4.2); Arrays.sort(student); //Äü¼ÒÆ® for(int i=0;i<5;i++) //toString¿¡ Á¤ÀÇµÈ Çü½ÄÀ¸·Î Ãâ·Â System.out.println(student[i]); } }
±âº» Á¤·Ä±âÁØ: Çйø ¿À¸§Â÷¼ø Á¤·Ä ¿Ï·á

¿©´ãÀ¸·Î ¸¸ÀÏ ComparableÀÌ ±¸ÇöµÇÁö ¾ÊÀº »óÅ¿¡¼­ Arrays.sort()¸¦ ÀÌ¿ëÇÏ¿© Á¤·ÄÀ» ¼öÇàÇÏ·Á ÇÏ¸é ¾Æ·¡¿Í °°Àº ¿¡·¯°¡ ¹ß»ýÇÏ°Ô µÈ´Ù.

ÀÌÀ¯´Â ¹Ù·Î Arrays.sort()µî Á¤·Ä ¸Þ¼­µåµéÀº Á¤·Ä ½Ã ÀÚµ¿À¸·Î Comparable¿¡ ±¸ÇöµÇ¾î ÀÖ´Â compareTo() ¸Þ¼­µå¸¦ È£ÃâÇؼ­ »ç¿ëÇϱ⠶§¹®ÀÌ´Ù.



2) Comparator
ComparableÀ» ÀÌ¿ëÇØ Çйø ¿À¸§Â÷¼ø´ë·Î Á¤·ÄÀ» ÇÏ¿´´Ù. ÀÌÁ¦ ÀÌ 5¸íÀÇ Çлýµé Áß¿¡ ¼ºÀû¿ì¼öÀÚ 2¸íÀ» ¼±Á¤ÇÏ¿© ÀåÇбÝÀ» Áö±ÞÇÏ·Á ÇÑ´Ù. ÀÌ ¶§, ¼ºÀûÀÌ °°Àº ÇлýÀÌ ¿©·¯ ¸íÀ̶ó¸é ÇйøÀÌ ºü¸¥ ¼ø¼­´ë·Î Á¤ÇÏ·Á°í ÇÑ´Ù.
¿ì¸®´Â ¼ºÀûÀÌ ³ôÀº Çлý 2¸íÀ» ÆíÇÏ°Ô Ã£±â À§Çؼ­ ÇлýµéÀ» "¼ºÀûÀÌ ³ôÀº ¼øÀ¸·Î Á¤·ÄÇÑ µÚ, ¾Õ¿¡¼­ 2¸íÀ» ¼±ÅÃ"ÇÏ·Á°í ÇÑ´Ù. ¾î¶»°Ô ÇÒ ¼ö ÀÖÀ»±î?

¼ºÀûÀÌ ³ôÀº ¼ø¼­´ë·Î Á¤·ÄÇÏ´Â °ÍÀÌ ¿ì¼±ÀÌ´Ù. 
±×·±µ¥ StudentŬ·¡½ºÀÇ ±âº» Á¤·Ä ±âÁØÀº "Çйø ¿À¸§Â÷¼ø"À̹ǷΠÀÌ ±âº» Á¤·Ä ±âÁØ°ú ´Ù¸¥ »õ·Î¿î Á¤·Ä ±âÁØÀ» ¼¼¿ö¾ß ÇÑ´Ù. ÀÌ ¶§ ÀÌ¿ëµÇ´Â °ÍÀÌ ¹Ù·Î ComparatorÀÌ´Ù.
Comparator´Â java.util.ComparatorÆÐÅ°Áö¿¡ ÀÖ°í, ÁÖ·Î À͸í Ŭ·¡½º·Î »ç¿ëµÇ¸ç, À̸¦ Arrays.sort()³»ºÎ¿¡ Á¤·Ä ±âÁØÀ¸·Î ±¸ÇöÇÏ¸é µÈ´Ù.

Arrays.sort() ³»ºÎ¿¡ Comparator¸¦ ±¸ÇöÇÒ ¼ö ÀÖµµ·Ï Á¤ÀǵǾî ÀÖ´Ù
Arrays.sort(student, new Comparator<Student>(){ //À͸íŬ·¡½º »ç¿ë, Á¦³Ê¸¯½º ÁÖÀÇ @Override public int compare(Student s1, Student s2) { // TODO Auto-generated method stub return 0; } });

ÀÌÁ¦ Á¤·Ä Á¶°ÇÀ» ´Ù½ÃÇѹø È®ÀÎÇغ¸¸é ¾Æ·¡¿Í °°´Ù.
  1) ÇÐÁ¡ÀÌ ³ôÀº ¼ø¼­´ë·Î(ÇÐÁ¡ ³»¸²Â÷¼ø)
  2) ÇÐÁ¡ÀÌ °°´Ù¸é ÇйøÀÌ ºü¸¥ ¼ø¼­´ë·Î(Çйø ¿À¸§Â÷¼ø)

À̸¦ ¹ÙÅÁÀ¸·Î ÇÐÁ¡ ºñ±³ ½ÄÀ» Comparator ³»ºÎ¿¡ ±¸ÇöÇÏ¸é ¾Æ·¡¿Í °°´Ù. À§ÀÇ Integer.compare ¼³¸í¿¡¼­ Àá±ñ ¾ð±ÞÇßµíÀÌ ³»¸²Â÷¼ø Á¤·ÄÀ» ÇÏ·Á¸é ¸Å°³º¯¼öÀÇ ¼ø¼­¸¦ ¹Ù²ãÁÖ¸é µÈ´Ù.

Arrays.sort(student, new Comparator<Student>(){ @Override public int compare(Student s1, Student s2) { double s1Score = s1.score; double s2Score = s2.score; return Double.compare(s2Score, s1Score);//ÇÐÁ¡ ³»¸²Â÷¼ø } });

±Ùµ¥ ÀÌ·¸°Ô¸¸ ³¡³ª¸é ¾ÈµÈ´Ù. ¿Ö³ÄÇϸé 4.5Á¡ ÇлýÀÌ 1¸í, 4.2Á¡ ÇлýÀÌ 2¸íÀ̹ǷÎ, ÇйøÀ» ÅëÇØ 2¼øÀ§¿Í 3¼øÀ§¸¦ ±¸º°ÇØ¾ß Çϱ⠶§¹®ÀÌ´Ù.

4.2Á¡ ÇлýÀÌ 2¸í Á¸Àç -> 2¼øÀ§, 3¼øÀ§ ±¸º° ÇÊ¿ä


µû¶ó¼­ 2¹ø° Á¶°ÇÀÎ 'ÇÐÁ¡ÀÌ °°À¸¸é Çйø ¿À¸§Â÷¼ø' Á¤·Ä Á¶°ÇÀ» Comparator ³»ºÎ¿¡ ±¸ÇöÇØÁÖ¸é ¾Æ·¡¿Í °°´Ù.

Arrays.sort(student, new Comparator<Student>(){ @Override public int compare(Student s1, Student s2) { double s1Score = s1.score; double s2Score = s2.score; if(s1Score == s2Score){ //ÇÐÁ¡ÀÌ °°À¸¸é return Double.compare(s1.id, s2.id); //Çйø ¿À¸§Â÷¼ø } return Double.compare(s2Score, s1Score);//ÇÐÁ¡ ³»¸²Â÷¼ø } });


¿©±â±îÁö ¿Ï¼ºµÈ Comparator¸¦ ¹ÙÅÁÀ¸·Î Á¤·ÄÀ» ¼öÇàÇÑ µÚ °á°ú¸¦ Ãâ·ÂÇÏ¸é ¾Æ·¡¿Í °°ÀÌ Á¤»ó Ãâ·ÂµÇ´Â °ÍÀ» º¼ ¼ö ÀÖ´Ù.

import java.lang.Comparable; import java.util.Arrays; import java.util.Comparator; //Comparator »ç¿ëÇϱâ À§ÇÑ import class Student implements Comparable<Student> { String name; //À̸§ int id; //Çйø double score; //ÇÐÁ¡ public Student(String name, int id, double score){ this.name = name; this.id = id; this.score = score; } public String toString(){ return "À̸§: "+name+", Çйø: "+id+", ÇÐÁ¡: "+score; } /* ±âº» Á¤·Ä ±âÁØ: Çйø ¿À¸§Â÷¼ø */ public int compareTo(Student anotherStudent) { return Integer.compare(id, anotherStudent.id); } } public class Main{ public static void main(String[] args) { Student student[] = new Student[5]; //¼ø¼­´ë·Î "À̸§", Çйø, ÇÐÁ¡ student[0] = new Student("Dave", 20120001, 4.2); student[1] = new Student("Amie", 20150001, 4.5); student[2] = new Student("Emma", 20110001, 3.5); student[3] = new Student("Brad", 20130001, 2.8); student[4] = new Student("Cara", 20140001, 4.2); Arrays.sort(student, new Comparator<Student>(){ public int compare(Student s1, Student s2) { double s1Score = s1.score; double s2Score = s2.score; if(s1Score == s2Score){ //ÇÐÁ¡ÀÌ °°À¸¸é return Double.compare(s1.id, s2.id); //Çйø ¿À¸§Â÷¼ø } return Double.compare(s2Score, s1Score);//³»¸²Â÷¼ø } }); for(int i=0;i<5;i++) System.out.println(student[i]); } }
»õ·Î¿î Á¤·Ä±âÁØ: ÇÐÁ¡ ³»¸²Â÷¼ø & °°À¸¸é Çйø ¿À¸§Â÷¼ø

¿©±â±îÁö Çؼ­ Comparable°ú Comparator¿¡ ´ëÇØ ¾Ë¾Æº¸¾Ò´Ù. »ó´çÈ÷ ÀÚÁÖ »ç¿ëµÇ´Â °³³äµéÀ̹ǷΠ½º½º·Î ´Ù¾çÇÏ°Ô »ç¿ëÇغ¸¸é¼­ ÀÍÈ÷´Â °ÍÀÌ Áß¿äÇÏ´Ù.


¡Ø º¸³Ê½º ¹®Á¦
Comparator¸¦ ÀÌ¿ëÇØ ÇлýµéÀ» À̸§ÀÇ ¿À¸§Â÷¼ø(=»çÀü ¼ø)À¸·Î Á¤·ÄÇÏ´Â Äڵ带 Â¥ º¸µµ·Ï ÇÏÀÚ
(Hint: StringŬ·¡½ºµµ ComparableÀÌ ±¸ÇöµÇ¾î ÀÖ´Ù. Áï ³»ºÎ¿¡ compareTo°¡ ±¸ÇöµÇ¾î ÀÖ....)

ÀÛ¼ºÀÚ ´ä¾È: