24 April 2016

Phân Biệt Comparator vs Comparable - Sort List Collection trên Java

SlideSort CompareTo
package demo;

import java.util.ArrayList;
import java.util.List;

public class SortList {

    public static void main(String[] args) {
        List<String> list = new ArrayList();
        list.add("B");
        list.add("E");
        list.add("C");
        list.add("D");
        list.add("A");

        System.out.println(list);
        
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < i; j++) {
                if (list.get(j).compareTo(list.get(i)) > 0) {
                    String a = list.get(j);
                    list.set(j, list.get(i));
                    list.set(i, a);
                }
            }
        }
        for (Object s : list) {
            System.out.println(s.toString());
        }

    }
}
Collection Sort
package demo;

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

public class SortListConllection {

    public static void main(String[] args) {
        Student a = new Student("Z", "FR", 7);
        Student b = new Student("B", "GM", 6);
        Student c = new Student("F", "AM", 3);
        Student d = new Student("K", "CN", 1);
        Student e = new Student("A", "LA", 4);
        Student f = new Student("E", "HU", 9);

        List<Student> list = new ArrayList();
        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        list.add(e);
        list.add(f);

        Collections.sort(list, new Student());
        for (Student s1 : list) {
            System.out.println(s1.getNamme());
        }
        for (Student s2 : list) {
            System.out.print(s2.getMoney() + " ");
        }

    }
}
Comparable vs Comparator
package demo;

import java.util.Comparator;

public class Student implements Comparable<Student>, Comparator<Student> {

    private String namme;
    private String location;
    private int money;
    private String check;

    public String getNamme() {
        return namme;
    }

    public void setNamme(String namme) {
        this.namme = namme;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getCheck() {
        return check;
    }

    public void setCheck(String check) {
        this.check = check;
    }

    public Student(String namme, String location, int money) {
        super();
        this.namme = namme;
        this.location = location;
        this.money = money;
    }

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return super.hashCode();
    }

    @Override
    public int compare(Student a, Student b) {
        // TODO Comparator<Student>
        return a.getMoney() - b.getMoney();
    }

    @Override
    public int compareTo(Student a) {
        // TODO Comparable<Student>
        return (this.namme).compareTo(a.getNamme());
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (!(obj instanceof Student)) {
            return false;
        }
        Student s = (Student) obj;
        if (s.getNamme() == null || s.getLocation() == null) {
            s.setCheck("bang");
            check = "bang";
            return true;

        }
        s.setCheck("a");
        check = "b";
        return false;
    }

}

Comparator
package demo;

import java.util.Comparator;

public class Student implements Comparator<Student> {

    private String namme;
    private String location;
    private int money;
    private String check;

    public String getNamme() {
        return namme;
    }

    public void setNamme(String namme) {
        this.namme = namme;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getCheck() {
        return check;
    }

    public void setCheck(String check) {
        this.check = check;
    }

    public Student(String namme, String location, int money) {
        super();
        this.namme = namme;
        this.location = location;
        this.money = money;
    }

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public int compare(Student a, Student b) {
        // TODO Comparator<Student>
        return a.getMoney() - b.getMoney();
    }

}
Test Comparator
package demo;

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

public class SortListConllection {

    public static void main(String[] args) {
        Student a = new Student("Z", "FR", 7);
        Student b = new Student("B", "GM", 6);
        Student c = new Student("F", "AM", 3);
        Student d = new Student("K", "CN", 1);
        Student e = new Student("A", "LA", 4);
        Student f = new Student("E", "HU", 9);

        List<Student> list = new ArrayList();
        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        list.add(e);
        list.add(f);

        Collections.sort(list, new Student());
        for (Student s1 : list) {
            System.out.println(s1.getNamme());
        }
        for (Student s2 : list) {
            System.out.print(s2.getMoney() + " ");
        }

    }
}
Comparable
package demo;

public class Student implements Comparable<Student>  {

    private String namme;
    private String location;
    private int money;
    private String check;

    public String getNamme() {
        return namme;
    }

    public void setNamme(String namme) {
        this.namme = namme;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getCheck() {
        return check;
    }

    public void setCheck(String check) {
        this.check = check;
    }

    public Student(String namme, String location, int money) {
        super();
        this.namme = namme;
        this.location = location;
        this.money = money;
    }

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public int compareTo(Student a) {
        return (this.namme).compareTo(a.getNamme());
    }


}
Test Comparable
package demo;

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

public class SortListConllection {

    public static void main(String[] args) {
        Student a = new Student("Z", "FR", 7);
        Student b = new Student("B", "GM", 6);
        Student c = new Student("F", "AM", 3);
        Student d = new Student("K", "CN", 1);
        Student e = new Student("A", "LA", 4);
        Student f = new Student("E", "HU", 9);

        List<Student> list = new ArrayList();
        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        list.add(e);
        list.add(f);

        Collections.sort(list);
        for (Student s1 : list) {
            System.out.println(s1.getNamme());
        }
        for (Student s2 : list) {
            System.out.print(s2.getMoney() + " ");
        }

    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang