使用Comparable接口按年龄给学生对象排序

package com.web;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

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

public class Test37 {
    
    
    public static void main(String[] args) {
    
    
        Student student = new Student("zhangsan", 12);
        System.out.println("student = " + student);//student = Student{name='zhangsan', age=12}
        char a = '{';
        String b = "{";
        System.out.println(a + '}');//248
        System.out.println(a);//{
    
    
        System.out.println('{');//{
    
    
        System.out.println(b + "}");//{}

        // 创建四个学生对象 存储到集合中
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(new Student("rose", 18));
        list.add(new Student("jack", 16));
        list.add(new Student("abc", 16));
        list.add(new Student("ace", 17));
        list.add(new Student("mark", 16));
         /*
          让学生 按照年龄排序 升序
         */
//        Collections.sort(list);//要求 该list中元素类型  必须实现比较器Comparable接口
        Collections.sort(list);
        for (Student stu : list) {
    
    
            System.out.println("student = " + stu);
        }
        //student = Student{name='jack', age=16}
        //student = Student{name='abc', age=16}
        //student = Student{name='mark', age=16}
        //student = Student{name='ace', age=17}
        //student = Student{name='rose', age=18}
        System.out.println("++++++++++++++++++++++++++++++++");
        //想要独立的定义规则去使用 可以采用Collections.sort(List list,Comparetor<T> c)方式,自己定义规则:
        Collections.sort(list, new Comparator<Student>() {
    
    
            @Override
            public int compare(Student o1, Student o2) {
    
    
                return o2.getAge() - o1.getAge();//以学生的年龄降序
            }
        });
         //顺序没有改变why????????
        //sort只能被重写一次
        //调用的还是升序sort(list)
        //Collections.sort(list);
        Collections.sort(list);
        for (Student stu2 : list) {
    
    
            System.out.println("student2 = " + stu2);
        }
    }
}

class Student implements Comparable<Student> {
    
    
    private String name;
    private int age;

    public Student(String name, int age) {
    
    
        this.name = name;
        this.age = 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 toString1() {
    
    
        return "Student{ name='" + name + "'" +
                ", age=" + age +
                "}";
    }

    //单引号,转义字符的用法
    // char a = '}';
    // String b = "}";
    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
    
    
        return this.age - o.age;//升序

    }
}

sort()只能重写一次,换个类重写sort()规则,降序才生效

package com.web;

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

public class Test38 {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<Student2> list = new ArrayList<Student2>();
        list.add(new Student2("rose", 18));
        list.add(new Student2("jack", 16));
        list.add(new Student2("abc", 16));
        list.add(new Student2("ace", 17));
        list.add(new Student2("mark", 16));

        Collections.sort(list, new Comparator<Student2>() {
    
    
            @Override
            public int compare(Student2 o1, Student2 o2) {
    
    
                return o2.getAge() - o1.getAge();//以学生的年龄降序
            }
        });
        //顺序没有改变why????????
        //sort只能被重写一次
        Collections.sort(list);
        for (Student2 stu2 : list) {
    
    
            System.out.println("student2 = " + stu2);
        }
        //student2 = Student2{name='rose', age=18}
        //student2 = Student2{name='ace', age=17}
        //student2 = Student2{name='jack', age=16}
        //student2 = Student2{name='abc', age=16}
        //student2 = Student2{name='mark', age=16}
    }
}

class Student2 implements Comparable<Student2> {
    
    
    private String name;
    private int age;

    public Student2(String name, int age) {
    
    
        this.name = name;
        this.age = 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;
    }

    @Override
    public String toString() {
    
    
        return "Student2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student2 o) {
    
    
        return 0;
    }
}

Guess you like

Origin blog.csdn.net/djydjy3333/article/details/121509930