Home Comparator & Comparable
Post
Cancel

Comparator & Comparable

Comparator & Comparable ๐Ÿง™โ€โ™‚๏ธ

Comparable โ€œ์ž๊ธฐ ์ž์‹ ๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด๋ฅผ ๋น„๊ต + compareTo ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„โ€ ์„ ์ˆ˜ vs ์„ ์ˆ˜
Comparator โ€œ๋‘ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด๋ฅผ ๋น„๊ตโ€ ์„ ์ˆ˜ vs ์„ ์ˆ˜ ์‚ฌ์ด์˜ ์‹ฌํŒ
๊ณตํ†ต์ ์œผ๋กœ๋Š” ๋น„๊ตํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด์ง€๋งŒ ์‹ค์งˆ์ ์œผ๋กœ๋Š” ๋น„๊ต๋Œ€์ƒ์ด ๋‹ค๋ฆ„.

Comparable ์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด compareTo ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜(Override/๊ตฌํ˜„)์„ ํ•ด์•ผํ•จ.
Comparator ์—์„œ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์€ compare(T o1,T o2) ๋ฉ”์„œ๋“œ์ด๋‹ค.

Comparable

์ž๋ฐ”์— ๊ธฐ๋ณธ์œผ๋กœ ์ ์šฉ๋œ Comparable์„ ๋œฏ์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

1
2
3
4
5
6
7
8
9
public final class Integer extends Number implements Comparable<Integer> {
  public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }
    //๊ฐ™์œผ๋ฉด 0 ์ž‘์œผ๋ฉด 1 ํฌ๋ฉด -1
    public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }
}

์ฃผ์„์—๋„ ์„ค๋ช…์ด ์žˆ๋“ฏ์ด ๊ฐ™์œผ๋ฉด 0 ์ž‘์œผ๋ฉด 1 ํฌ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฒƒ์„ ๋ณผ์ˆ˜ ์žˆ๋‹ค.
๊ทธ๋ ‡๋‹ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•  ๋–„๋Š” ์–ด๋–จ๊นŒ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Student class์— age์™€ classNumber๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •.

class Student implements Comparable<Student> {
 
	int age;			// ๋‚˜์ด
	int classNumber;	// ๋ฐ˜ ๋ฒˆํ˜ธ
	
	Student(int age, int classNumber) {
		this.age = age;
		this.classNumber = classNumber;
	}
    //์ •์„
    @Override
	public int compareTo(Student o) {
    
		// ์ž๊ธฐ์ž์‹ ์˜ age๊ฐ€ o์˜ age๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์–‘์ˆ˜
		if(this.age > o.age) {
			return 1;
		}
		// ์ž๊ธฐ ์ž์‹ ์˜ age์™€ o์˜ age๊ฐ€ ๊ฐ™๋‹ค๋ฉด 0
		else if(this.age == o.age) {
			return 0;
		}
		// ์ž๊ธฐ ์ž์‹ ์˜ age๊ฐ€ o์˜ age๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์Œ์ˆ˜
		else {
			return -1;
		}
	}
    //์•ผ๋งค? 
    @Override
	public int compareTo(Student o) {
		return this.age - o.age; 
	}
}

์ •์„์˜ ๋ฐฉ๋ฒ•๊ณผ ์•ผ๋งค์Šค๋Ÿฌ์šด ๋ฐฉ๋ฒ•์œผ๋กœ compareTo๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค. ์ˆซ์ž๋ฅผ ๋น„๊ตํ• ๋–„ ํฐ์ˆ˜๋Š” ๋นผ๋ฉด ์Œ์ˆ˜๊ฐ€๋˜๊ณ  ์ž‘์€์ˆ˜๋Š” ์–‘์ˆ˜๊ฐ€ ๋˜๋Š” ์ ์„ ์ด์šฉํ•ด์„œ ์ง  ์ฝ”๋“œ์ด๋‹ค. ๋‹ค๋งŒ ์ € ์•ผ๋งค์—๋Š” ํ•จ์ •์ด์žˆ๋‹ค. int ์ž๋ฃŒํ˜•์ฒ˜๋Ÿผ ํ‘œํ˜„๋ฒ”์œ„๊ฐ€ 21์–ต47~๊นŒ์ง€ ๋˜์–ด์žˆ์–ด ๊ทธ ์ด์ƒ, ๊ทธ ์ดํ•˜์˜ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋‚˜ ์–ธ๋”ํ”Œ๋กœ์šฐ๋กœ ์ธํ•ด ๊ฐ’์ด ์ž˜๋ชป ๋ฐ˜ํ™˜ ๋  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌํ•˜์—ฌ ์ด๋Ÿฐ ์ ์€ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •์„์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด ์‚ฌ์ „์— ๋ณด์™„ํ•  ์ˆ˜ ์žˆ๋‹ค.

Comparator

Comparable์˜ compareTo()์™€ ๋‹ค๋ฅด๊ฒŒ Comparator์˜ compare์€ ๋‘ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•œ๋‹ค. ์ด๋ฒˆ์—๋Š” ์•„๊นŒ ์‚ฌ์šฉํ•œ student ๊ฐ์ฒด์—์„œ classNumber์„ ๋น„๊ตํ•˜๊ณ ์ž ํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Student class์— age์™€ classNumber๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •.

class Student implements Comparator<Student> {
 
	int age;			// ๋‚˜์ด
	int classNumber;	// ๋ฐ˜ ๋ฒˆํ˜ธ
	
	Student(int age, int classNumber) {
		this.age = age;
		this.classNumber = classNumber;
	}
    @Override
	public int compare(Student o1, Student o2) {
    
		if(o1.classNumber > o2.classNumber) {
			return 1;
		}
		else if(o1.classNumber == o2.classNumber) {
			return 0;
		}
		else {
			return -1;
		}
	}
    //์•ผ๋งค?
    @Override
	public int compare(Student o1, Student o2) {
		return o1.classNumber - o2.classNumber;
	}
}

๋งจ ์œ„์—์„œ ์‹ฌํŒ์ด๋ผ๋Š” ํ‘œํ˜„์ฒ˜๋Ÿผ o1๊ณผ o2์™€ ๋น„๊ต๋ฅผ ํ•  ๋ฟ, Student๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์—๋Š” ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.
comparable์—์„œ์ฒ˜๋Ÿผ int ๋ฒ”์œ„๋‚ด๋ฅผ ๋„˜์ง€์•Š๋Š”๋‹ค๋ฉด ์•ผ๋งค๋ฐฉ๋ฒ•์„ ์จ๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.
์‚ฌ์‹ค ๋ณ„์ฐจ์ด์—†์–ด๋ณด์ด๋Š”๋ฐ Comparator์˜ ์žฅ์ ์€ ๋ฌด์—‡์ผ๊นŒ? ๋ฐ”๋กœ ์ต๋ช…ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ์ปค์Šคํ…€๋งˆ์ด์ง•ํ•œ ๋น„๊ต๋ฅผ ํ• ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    public static void main(String[] args)  {
 
		Student a = new Student(17, 2);	// 17์‚ด 2๋ฐ˜
		Student b = new Student(18, 1);	// 18์‚ด 1๋ฐ˜
		Student c = new Student(15, 3); // 15์‚ด 3๋ฐ˜

        int i = classNumberCompare.compare(a,b);
        if(i >0){...}
        else if(i <0){...}
        else {...}

        int j = ageCompare.compare(a,b);
        if(j >0){...}
        else if(j <0){...}
        else {...}
    }

    //ํ•™์ƒ์„ ๋ฐ˜๋ณ„, ๋‚˜์ด๋ณ„๋กœ ๋น„๊ตํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ๋‹ค. ์ด๋Ÿด๋• ์ต๋ช…ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ์ปค์Šคํ…€๋งˆ์ด์ง•ํ•œ ๋น„๊ต๋ฅผ ํ• ์ˆ˜ ์žˆ๋‹ค.

    public static Comparator<Student> classNumberCompare= new Comparator<Student>() {
		@Override
		public int compare(Student o1, Student o2) {
			return o1.classNumber - o2.classNumber;
		}
	};
	
	public static Comparator<Student> ageCompare = new Comparator<Student>() {
		@Override
		public int compare(Student o1, Student o2) {
			return o1.age - o2.age;
		}
	};

๋‹ค์–‘ํ•œ ์ถœ๋ ฅ์˜ ์˜ˆ๋ฅผ ๋ชจ์•„๋ดค๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.*;

public class comparatorTest {
    public static void main(String[] args) {
        String[] animals = {"cat", "Dog", "lion", "Tiger"};
        List<String> animal_list = new ArrayList<String>(Arrays.asList(animals));


        Arrays.sort(animals); //Comparable์˜ ์ •์˜
        System.out.println("1 Arrays ์ •๋ ฌ= "+ Arrays.toString(animals));
        Collections.sort(animal_list);
        System.out.println("2 ์ปฌ๋ž™์…˜ ์ •๋ ฌ = "+ animal_list);

        Arrays.sort(animals, String.CASE_INSENSITIVE_ORDER); //๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„์•ˆํ•˜๊ณ  sort
        System.out.println("3 = "+ Arrays.toString(animals));

        Arrays.sort(animals, new Desending());
        System.out.println("4 = "+ Arrays.toString(animals));

        Collections.sort(animal_list, (o1,o2)->{
            if(o1 instanceof Comparable && o2 instanceof Comparable){
                Comparable c1 = (Comparable)o1;
                Comparable c2 = (Comparable) o2;
            return c1.compareTo(c2)*-1;}
            else return -1;
        });
        System.out.println("5 = "+ animal_list);
    }

    private static class Desending implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            return -(o1.compareTo(o2));
            }
        }
}
// 1 Arrays ์ •๋ ฌ= [Dog, Tiger, cat, lion]
// 2 ์ปฌ๋ž™์…˜ ์ •๋ ฌ = [Dog, Tiger, cat, lion]
// 3 = [cat, Dog, lion, Tiger]
// 4 = [lion, cat, Tiger, Dog]
// 5 = [lion, cat, Tiger, Dog]

์ถœ์ฒ˜

  • ์˜ค๋ผํด/์ž๋ฐ”/8
  • ์ž๋ฐ”์˜ ์ •์„
This post is licensed under CC BY 4.0 by the author.

ํ–‰์œ„ํŒจํ„ด - ์ „๋žตํŒจํ„ด๊ณผ ์ƒํƒœ ํŒจํ„ด

ํ–‰์œ„ํŒจํ„ด - ์ดํ„ฐ๋ ˆ์ดํ„ฐ ํŒจํ„ด