Popular Posts

이은한. Powered by Blogger.

2022년 2월 7일 월요일

what is diffence between comparator and compatable


what is diffence between comparator and compatable in JAVA

Question

Both interfaces are very similer because they are for sorting lists and arrays. However, one is for one condition and other one is for mutiple conditions.

Conclusion

use comparator for unique multiple conditioned sorting
use comparable for one conditioned sorting in Object

It is possible to put mutiple conditions into the comparable. However, it may cause some confustion because it will override the compareTo() method.

Explanation

Comparable

  • java.lang package
  • Comparable affects the original class because override the compareTo method.
  • compareTo() method with 1 parameter
  • Collections.sort(List)
  • Arrays.sort(array)
  • It defines sort condition for object. It built in to the object. (Collections.sort(List))
  • For example, if you sort your object by mutiple conditions with compatable, you cannot sort simple ascending order because overrided the comareTo method.

Comparator

  • java.util package.
  • Comparator doesn't affect the original class because it create compare method.
  • compare() method with 2 parameters
  • Collections.sort(List, Comparator)
  • Arrays.sort(array, myComparator);
  • For example, if you sort your object by mutiple conditions with comparator, you still can sort simple ascending order with Collections.sort.

Example

custom class with compatable


import java.lang.Comparable;

public class customPoint implements Comparable<customPoint> {

    private int x = 0;
    private int y = 0;

    public customPoint(int x, int y) {
        this.x = x;
        this.y = y;

    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public int compareTo(customPoint p2) {
        if (this.x > p2.x) {
            return 1; // x in ascending order
        } else if (this.x == p2.x) {
            if (this.y < p2.y) { 
// x is ascending order and then y is in descending order
                return 1;
            }
        }
        return -1;
    }
}

comparator


import java.awt.*;
import java.util.Comparator;

public class MyComparator  implements Comparator<Point> {

    public int compare(Point p1, Point p2) {
        if (p1.x > p2.x) {
            return 1; //  x in ascending order
        } else if (p1.x == p2.x) {
            if (p1.y < p2.y) {
// x is ascending order and then y is in descending order
                return 1;
            }
        }
        return -1;
    }
}

main


import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        java.util.List<Point> pointList = new ArrayList<Point>();
        pointList.add(new Point(10, 10));
        pointList.add(new Point(1, 8));
        pointList.add(new Point(5, 2));
        pointList.add(new Point(1, 2));
        pointList.add(new Point(5, 5));
        pointList.add(new Point(10, 1));

        System.out.println("current list");
        for (Point temp : pointList) {
            System.out.println("x: " + temp.x + "  " + "y: " + temp.y);
        }
        System.out.println("Comparator sorted list");
        MyComparator myComparator = new MyComparator();
        Collections.sort(pointList, myComparator);
        for (Point temp : pointList) {
            System.out.println("x: " + temp.x + "  " + "y: " + temp.y);
        }
        System.out.println("====================");

        List<customPoint> pointList2 = new ArrayList<>();
        pointList2.add(new customPoint(10, 10));
        pointList2.add(new customPoint(1, 8));
        pointList2.add(new customPoint(5, 2));
        pointList2.add(new customPoint(1, 2));
        pointList2.add(new customPoint(5, 5));
        pointList2.add(new customPoint(10, 1));

        System.out.println("current list");
        for (customPoint temp : pointList2) {
            System.out.println("x: " + temp.getX() + "  " + "y: " + temp.getY());
        }

        System.out.println("Comparable sorted list");
        Collections.sort(pointList2);
        for (customPoint temp : pointList2) {
            System.out.println("x: " + temp.getX() + "  " + "y: " + temp.getY());
        }

    }
}

result


current list
x: 10  y: 10
x: 1  y: 8
x: 5  y: 2
x: 1  y: 2
x: 5  y: 5
x: 10  y: 1
Comparator sorted list
x: 1  y: 8
x: 1  y: 2
x: 5  y: 5
x: 5  y: 2
x: 10  y: 10
x: 10  y: 1
==============================
current list
x: 10  y: 10
x: 1  y: 8
x: 5  y: 2
x: 1  y: 2
x: 5  y: 5
x: 10  y: 1
Comparable sorted list
x: 1  y: 8
x: 1  y: 2
x: 5  y: 5
x: 5  y: 2
x: 10  y: 10
x: 10  y: 1

0 개의 댓글:

댓글 쓰기