By GokiSoft.com| 15:00 11/03/2020|
Java Basic

Share Code- Chia sẻ code java overview - Collections.sort trong java

Class Student



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

/**
 *
 * @author Diep.Tran
 */
public class Student {
    String fullname, address;
    int age;

    public Student() {
    }

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

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", age=" + age + '}';
    }
    
    
}
Class Test



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

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

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        //Overview Collections.sort
        //Muc dich => sap xep mang theo trat tu cua de bai yeu la gi??
        //sap xep tang dan, giam dan.
        //Loai 1: Mang cua cac ban la Integer, String, Float,...
        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(30);
        list.add(13);
        list.add(31);
        list.add(300);
        
        //mang nay chua dduoc sap xep
        //Sap xep theo thu tu tang dan => lam nhu the nao
        //Ngon ngu lap trinh C => su dung 2 vong for => giai thuat sap
        //Nhung vs Java, C#, PHP => da co thu vien san => cac ban chi can hoc cach dung
        Collections.sort(list);//sap xep de nhat
        
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //Sap xet giam dan
        Collections.reverse(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //Xong phan su dung Collections.sort trong bai toan sap xep tang dan & giam dan
        //Phan 2: Sap xep 1 mang => class object => sap xep tang dan theo 1 thuoc tinh cuar
        //doi tuong (object) => thi chung ta se lam nhu the nao?
        //Gia su tao ra class object Student gom cac thuoc tinh : ten, tuoi, dia chi
        List<Student> studentList = new ArrayList<>();
        Student std1 = new Student("TRAN VAN A", "HA NOI", 20);
        studentList.add(std1);
        
        Student std2 = new Student("TRAN VAN B", "NAM DINH", 25);
        studentList.add(std2);
        
        Student std3 = new Student("TRAN VAN C", "NAM DINH", 21);
        studentList.add(std3);
        //studentList >> gom 2 sinh vien >> sap xep lung tung
        //De bai => sap xep mang studentList => theo thu tu tang dan ve tuoi
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getAge() > o2.getAge()) {
                    return 1;
                }
                return -1;
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            System.out.println(studentList.get(i).toString());
        }
        
        
        //Sap xep mang studentList theo thu tu A-Z theo ten
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getFullname().compareTo(o2.getFullname());
            }
        });
        
        for (int i = 0; i < studentList.size(); i++) {
            System.out.println(studentList.get(i).toString());
        }
        
        //Sap xep mang studentList theo thu tu Z-A theo ten
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return -o1.getFullname().compareTo(o2.getFullname());
            }
        });
        
        for (int i = 0; i < studentList.size(); i++) {
            System.out.println(studentList.get(i).toString());
        }
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)