• Thế Giới Giải Mã

    Bí ẩn nhân loại Leonardo Da Vinci

  • Thế Giới Giải Mã

    Anh hùng thầm lặng Nikola Tesla

  • Thế Giới Giải Mã

    Thần đèn Thomas Edison

  • Thế Giới Giải Mã

    Người thôi miên Adolf Hitler

Showing posts with label Java Collection. Show all posts
Showing posts with label Java Collection. Show all posts

11 June 2017

Sắp Xếp Nhiều Trường Bằng Comparable @Override CompareTo trong Java

Student.java
Java Core
package CompareSort;

public class Student implements Comparable < Student > {

    private String name;
    private String address;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Student(String name, String address, int age) {
        super();
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public Student() {
        super();
    }

    @Override
    public int compareTo(Student o) {
        int sizeName = this.name.compareTo(o.name);
        if (sizeName != 0) {
            return sizeName;
        }
        int sizeAddress = this.address.compareTo(o.address);
        if (sizeAddress != 0) {
            return sizeAddress;
        }
        return this.age - o.age;
    }

}
main.java
Java Core
package CompareSort;

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

public class Main {

    public static void main(String[] args) {
        Student a = new Student("A", "B", 6);
        Student b = new Student("C", "B", 5);
        Student c = new Student("C", "B", 3);
        Student d = new Student("A", "B", 1);
        Student e = new Student("C", "B", 4);
        Student f = new Student("A", "B", 7);

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

        Collections.sort(list);

        for (Student ss : list) {
            System.out.println(" Name: " + ss.getName() + " Address: " + ss.getAddress() + " Age: " + ss.getAge());
        }
    }
}
Trường hợp thay đổi giá trị thứ tự sắp xếp ưu tiên 
Name > Address > Age
 Nếu Name trùng nhau thì sẽ ưu tiến sắp xếp theo Address


Nếu Name và Address có tên trùng nhau thì ưu tiên cuối cũng sẽ sắp xếp theo Age

Sắp Xếp Nhiều Trường Bằng Comparator @Override Compare trong Java

Student.java
Java Core 2017
package CompareSort; import java.util.Comparator; public class Student implements Comparator<Student> { private String name; private String address; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Student(String name, String address, int age) { super(); this.name = name; this.age = age; this.address = address; } public Student() { super(); } @Override public int compare(Student o1, Student o2) { int sizeName = o1.name.compareTo(o2.name); //Sort name (String) if (sizeName != 0) { return sizeName; } int sizeAddress = o1.address.compareTo(o2.address); //Sort Address (String) if (sizeAddress != 0) { return sizeAddress; } return o1.age - o2.age; //Sort Age (int) } }
Main.java
Java Core 2017
package CompareSort; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) { Student a = new Student("A", "B", 6); Student b = new Student("C", "B", 5); Student c = new Student("C", "B", 3); Student d = new Student("A", "B", 1); Student e = new Student("C", "B", 4); Student f = new Student("A", "B", 7); ArrayList<Student> list = new ArrayList<Student>(); 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 ss : list) { System.out.println(" Name: " + ss.getName() + " Address: " + ss.getAddress() + " Age: " + ss.getAge()); } } }
Trường hợp thay đổi giá trị thứ tự sắp xếp ưu tiên
Name > Address > Age
 Nếu Name trùng nhau thì sẽ ưu tiến sắp xếp theo Address


Nếu Name Address có tên trùng nhau thì ưu tiên cuối cũng sẽ sắp xếp theo Age

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() + " ");
        }

    }
}

 

BACK TO TOP

Xuống cuối trang