JAVA_Comparable & Comparator
๐ JAVA Permalink

๐ Comparable & Comparator Permalink
Comparable & ComparatorPermalink
Comparable ๊ณผ Comparator ๋ ๋ชจ๋ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ ์ ์๊ฒ ํด์ฃผ๋ Interface ์ด๋ค.
๊ทธ๋์ Comparable ์ด๋ Comparator ๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ๋ค๋ฉด ์ธํฐํ์ด์ค ๋ด์ ์ ์ธ๋ ๋ฉ์๋๋ฅผ ๋ฐ๋์ ๊ตฌํํด์ผ ํ๋ค !
์ฐ๋ฆฌ๋ Primitive ํ์
์ ์ค์ ๋ณ์(byte, int ,double ๋ฑ๋ฑ)์ ๊ฒฝ์ฐ๋ ๋ถ๋ฑํธ๋ฅผ ๊ฐ์ง๊ณ ์ฝ๊ฒ ๋น๊ตํ ์ ์๋ค.
๊ทธ๋ฌ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋น๊ตํ๋ค๊ณ ์๊ฐํด๋ณด์. ๊ฐ์ฒด์ ๋์ด์ ์ด๋ฆ๊ณผ ์ฃผ์๊ฐ ์๋ค. ๊ทธ๋ผ ๋ค๋ฅธ ๊ฐ์ฒด์ ์ด๋ป๊ฒ ๋น๊ตํ ๊ฒ์ธ๊ฐ?
๊ฐ์ฒด๋ ๊ธฐ์ค์ ์ ํด์ฃผ์ง ์๋ ์ด์ ์ด๋ค ๊ฐ์ฒด๊ฐ ๋ ๋์ ์ฐ์ ์์๋ฅผ ๊ฐ๋์ง ํ๋จํ ์ ์๋ค.
์ด๋ฌํ ๋ฌธ์ ์ ์ ํด๊ฒฐํ๊ธฐ ์ํด Comparable ์ด๋ Comparator๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
์ฌ๊ธฐ์ ๋๊ฐ์ง์ ์ฐจ์ด์ ์ ๋ณธ์ง์ ์ผ๋ก ๋น๊ตํ๋ ๊ฒ ์์ฒด๋ ๊ฐ์ง๋ง ๋น๊ต ๋์์ด ๋ค๋ฅด๋ค๋ ๊ฒ์ด๋ค.
์์ธํ ๋ด์ฉ์ ๋ฐ์์ ๊ฐ๊ฐ ๋ค๋ค๋ณด๊ฒ ๋ค.
ComparablePermalink
Comparable ์ด๋ ๋ฌด์์ผ๊น?Permalink
Comparable์ ์๊ธฐ์์ ๊ณผ ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๋ค.
Comparable์์ ์ฌ์ฉํ ๋ฉ์๋๋ compareTo() ์ด๋ค.
์ด ๋ฉ์๋๊ฐ ์ฐ๋ฆฌ๊ฐ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ ๊ธฐ์ค์ ์ ์ํด์ฃผ๋ ๋ถ๋ถ์ด ๋๋ค.
์๊ธฐ์์ ๊ณผ ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๋ค.
Comparable ์ ๊ธฐ๋ณธ ๊ตฌ์กฐPermalink
public class ClassName implements Camparable<Type>{
// ๊ตฌํ
@Override
public int compareTo(Type o){
// ๋น๊ต ๊ตฌํ
}
}
Comparable ์ค์ตํด๋ณด์ฆ์!Permalink
public class ComparableTest {
public static void main(String[] args) {
Student s1 = new Student(15, "jisoo");
Student s2 = new Student(16, "hoho");
int value = s1.compareTo(s2); // s1 ์๊ธฐ์์ ๊ณผ s2 ๋ฅผ ๋น๊ตํ๋ค
if(value > 0) {
System.out.println("s1๊ฐ์ฒด๊ฐ s2๊ฐ์ฒด๋ณด๋ค ๋ ํฝ๋๋ค.");
}
else if(value == 0) {
System.out.println("s1๊ฐ์ฒด์ s2๊ฐ์ฒด๊ฐ ๊ฐ์ต๋๋ค.");
}
else {
System.out.println("s1๊ฐ์ฒด๊ฐ s2๊ฐ์ฒด๋ณด๋ค ๋ ์์ต๋๋ค.");
}
}
}
class Student implements Comparable<Student>{
int age;
String name;
Student(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public int compareTo(Student o) {
return this.age - o.age;
}
}
์คํ๊ฒฐ๊ณผPermalink
s1๊ฐ์ฒด๊ฐ s2๊ฐ์ฒด๋ณด๋ค ๋ ์์ต๋๋ค.
์ฝ๋ ๊ตฌ์กฐ ์ค๋ช Permalink
โ๊ฐโ์ ๋น๊ตํ์ฌ int๊ฐ์ ๋ฐํํ๋๋ก ํ์๋ค.
์๊ธฐ์์ ์ ๋์ด์ ๋น๊ตํ ๊ฐ์ฒด์ ๋์ด๋ฅผ ๋นผ์ ๊ทธ๊ฒ์ด ์์,0,์์ ์ผ ๋๋ง๋ค ๋ค๋ฅด๊ฒ ์ถ๋ ฅํ๋๋ก ํ์๋ค.
์ฆ, Comparable์ ์๊ธฐ ์์ ์ ๊ธฐ์ค์ผ๋ก ์ผ์ ๋์๊ด๊ณ๋ฅผ ํ์
ํ๋ค.
s1์ ๋์ด๊ฐ 15์ด๊ณ s2์ ๋์ด๊ฐ 16์ธ๋ฐ โ int value = s1.compareTo(s2); โ ์์ s1์์ s2๋ฅผ ๋น๊ตํ ๊ฒฐ๊ณผ๋ฅผ value๋ผ๋ intํ ๋ณ์์ ์ ์ฅํด์ฃผ์๋ค.
๊ทธ ๊ฒฐ๊ณผ -1์ด๋ฏ๋ก value๋ 0๋ณด๋ค ์์์ โs1๊ฐ์ฒด๊ฐ s2๊ฐ์ฒด๋ณด๋ค ๋ ์์ต๋๋ค.โ๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅํด์ฃผ๋ ๊ฒ์ด๋ค.
์ฌ๊ธฐ์ ๋ฌด์์ ์ถ๋ ฅํ๋๋ ๊ทธ๊ฒ์ ๋ง์๋๋ก์ด๋ค. ๊ทธ๋ฅ ๋ ๋น๊ต๋์์ ์ฐจ์ด๋ฅผ ๋ฐํํด๋ ๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋ง์ฝ ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์ํ๊ณ ์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ํ๊ณ ์ถ๋ค๋ฉด compartTo() ๋ฉ์๋์์ return ๊ฐ์ this.name - o.name ์ด๋ผ๊ณ ํ๋ฉด ๋๋ค.
์ฌ๊ธฐ์ this๋ s1 ๊ฐ์ฒด ์์ ์ ์๋ฏธํ๊ณ o๋ s2 ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค.
ComparatorPermalink
Comparator ์ด๋ ๋ฌด์์ผ๊น?Permalink
Comparator์ ๋ ๋งค๊ฐ๋ณ์ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๋ค.
Comparator์์ ์ฌ์ฉํ ๋ฉ์๋๋ compare() ์ด๋ค.
์ด ๋ฉ์๋๊ฐ ์ฐ๋ฆฌ๊ฐ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ ๊ธฐ์ค์ ์ ์ํด์ฃผ๋ ๋ถ๋ถ์ด ๋๋ค.
๋ ๋งค๊ฐ๋ณ์๋ฅผ ๋น๊ตํ๋ค.
Comparator์ โimport java.util.Comparatorโ ๊ฐ ๊ผญ ์์ด์ผ ํ๋ค !
Comparator ์ ๊ธฐ๋ณธ ๊ตฌ์กฐPermalink
import java.util.Comparator //import ํ์
public class ClassName implements Comparator<Type>{
// ๊ตฌํ
// ํ์ ๊ตฌํ ๋ถ๋ถ
@Override
public int compare(Type o1, Type o2){
// ๋น๊ต ๊ตฌํ
}
}
Comparator ์ค์ตํด๋ณด์ฆ์!Permalink
import java.util.Comparator;
public class ComparatorTest {
public static void main(String[] args) {
Students s1 = new Students(19, "jisoo");
Students s2 = new Students(17, "susu");
int values = s1.compare(s1, s2);
if (values > 0) {
System.out.println("s1๊ฐ์ฒด๊ฐ s2๊ฐ์ฒด๋ณด๋ค ํฝ๋๋ค");
} else if (values == 0) {
System.out.println("๋ ๊ฐ์ฒด์ ํฌ๊ธฐ๊ฐ ๊ฐ์ต๋๋ค.");
} else {
System.out.println("s1๊ฐ์ฒด๊ฐ s2๊ฐ์ฒด๋ณด๋ค ์์ต๋๋ค.");
}
}
}
class Students implements Comparator<Students> {
int age;
String name;
public Students(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public int compare(Students o1, Students o2) {
return o1.age - o2.age;
}
}
์คํ๊ฒฐ๊ณผPermalink
s1๊ฐ์ฒด๊ฐ s2๊ฐ์ฒด๋ณด๋ค ํฝ๋๋ค
์ฝ๋ ๊ตฌ์กฐ ์ค๋ช Permalink
Comparable๊ณผ ๊ตฌ์กฐ๋ ๊ฑฐ์ ๋น์ทํ๋ค.
๊ทธ๋ฌ๋ Comparator์ compare() ๋ฉ์๋์์ ๋งค๊ฐ๋ณ์๋ผ๋ฆฌ ๋น๊ต๋ฅผ ํ์ฌ ๊ฐ์ ๋ฐํํด์ค๋ค.
๊ทธ๋ฆฌ๊ณ โint values = s1.compare(s1, s2);โ ์ด ๋ถ๋ถ์์ compare() ๋ฉ์๋๋ฅผ ๋ถ๋ฌ์ฃผ๋ ๊ฐ์ฒด๋ ์๋ฌด๊ฑฐ๋ ์ฌ์ฉํด๋ ๋๋ค.
Comparable & Comparator ์ ์ ๋ ฌ๊ณผ์ ๊ด๊ณPermalink
JAVA์์์ ์ ๋ ฌ์ ํน๋ณํ ์ ์๊ฐ ๋์ด ์์ง ์๋ ํ โ์ค๋ฆ ์ฐจ์โ ๊ธฐ์ค์ผ๋ก ํ๋ค.
๋ ์์ ๋น๊ต ๊ฒฐ๊ณผ๊ฐ
- ์์์ผ ๊ฒฝ์ฐ : ๋ ์์์ ์์น๋ฅผ ๊ตํ ์ ํจ
- ์์์ผ ๊ฒฝ์ฐ : ๋ ์์์ ์์น๋ฅผ ๊ตํ ํจ
โ๋ด๋ฆผ ์ฐจ์โ ์ผ๋ก ํ๊ณ ์ถ๋ค๋ฉด ๋ถํธ๋ฅผ ๋ฐ๋๋ก ํด์ฃผ๋ฉด ๋๋ค .
๊ทธ๋ฆฌ๊ณ ์ข ๋ ์ฝ๊ฒ ์ดํด๋ฅผ ํ๋ ค๋ฉด ์ ํ ์์์ ๊ฐ์ ์๋ค๊ณ ๊ฐ์ ํ๊ณ ํํ ์์์ ๊ฐ์ ํฌ๋ค๊ณ ๊ฐ์ ํ๊ณ ์์, ์์ ๊ด๊ณ๋ฅผ ์ดํดํ๋ฉด ๋๋ค.
Comparable & Comparator ๋ง๋ฌด๋ฆฌPermalink
Comparable๊ณผ Comparator์ ๊ฐ์ฒด๋ค์ ๋น๊ตํ๊ณ ์ ํ๋ค๋ฉด ํ์์์์ด๋ค.
์ด์ ํ๋ก๊ทธ๋จ์ ๊ฐ๋ฐํ๊ณ ์ ํ๋ฉด ๊ฐ์ฒด๋ฅผ ์ค์ฌ์ผ๋ก ํ์ผ์ ๋๋๊ณ ๊ธฐ๋ฅ์ ๋ถ๋ฆฌํ์ฌ ํด๋์ค๋ฅผ ๋ฐ๋ก ๋ง๋๋ ๋ฑ ๊ฐ์ฒด์งํฅ ์ฝ๋ฉ์ ํด์ผํ๋ ๊ฒฝ์ฐ๊ฐ ํ๋คํ ๊ฒ์ด๋ค.
๊ทธ ๊ณผ์ ์์ ๊ฐ์ฒด์ ๋น๊ต๋ฅผ ์ ์ํ๋ ๊ฒ์ ๋ฐ๋์ ์์๋์ด์ผ ํ๋ค !!
๊ทธ๋ฆฌ๊ณ ์ฌ์ค ์ฌ๋ฌ ๋น๊ตํ๋ ๋ฉ์๋์์๋ Comparable์ด๋ Comparator๊ฐ ์ฌ์ฉ๋๋ ๊ฒ์ผ๋ก ์๊ณ ์๋ค. ๊ทธ ๋ป์ ๊ณ์ ์ฌ์ฉํ๊ณ ์์๋ค๋ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ํนํ ์ ๋ ฌํ ๋ ๋ง์ด ์ฐ์ธ๋ค. ์ ์ผ ๋ํ์ ์ธ ์ ๋ ฌํ๋ ๋ฉ์๋์ธ sort()๋ Comparable๋ก ๊ตฌ์ฑ๋์ด ์๋ค.
๋ง์ฝ sort()๊ธฐ๋ฅ ๋ฟ๋ง ์๋๋ผ ๋ ๋์๊ฐ ์ฌ์ฉ์๊ฐ ์ํ๋ ์ ๋ ฌ ์กฐ๊ฑด์ ์ฃผ๊ณ ์ถ์ ๋๋ ๊ตฌ์กฐ๋ฅผ ์์์ผ ์๋ง๊ฒ ์ค ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด์ด๋ list๋ฅผ ์ ๋ ฌํ ๋๋ ์ข
์ข
์ฐ์ด๊ณค ํ๋ค.
๊ทธ๋ฌ๋ฏ๋ก ํ์คํ๊ฒ ๊ฐ๋
๊ณผ ๊ตฌ์กฐ๋ฅผ ์๊ณ ์ง์ ๊ตฌํํ ์๋ ์์ด์ผ ์๊ฐํ๋ค.
๊ทธ๋ฆฌ๊ณ ๋ง์ง๋ง ํ ๊ฐ์ง ์ฃผ์ํ ์ ์ Comparable ๊ณผ Comparator ๋ฅผ ๊ตฌํํ ๋ Overflow์ Underflow๋ฅผ ๊ผญ ๊ณ ๋ คํ์ฌ์ผ ํ๋ค !!
๐ ์ฐธ์กฐ
https://st-lab.tistory.com/243