List集合排序实体类实现Comparator接口和实现Comparable接口排序

  1. package com.collection;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    /**
     * 容器排序
     * @author 26920
     *
     */
    public class TestList {
    	public static void main(String[] args) {
    		List list=new ArrayList();
    		list.add(new Student());
    		list.add(new Student("wangwu",20,90));
    		list.add(new Student("lisi",17,80));
    		list.add(new Student("zhangliu",18,60));
    		list.add(new Student("wangba",15,70));
    		for (Student student : list) {
    			System.out.println(student.toString());
    		}
    		System.out.println("排序后===========================");
    		Collections.sort(list, new Student());
    		//list.sort(new Student());等价于Collections.sort(list, new Student());
    		for (Student student : list) {
    			System.out.println(student.toString());
    		}
    	}
    }
    class Student implements Comparator{
    	private String name;
    	private int age;
    	private double grade;
    	public Student(String name, int age, double grade) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.grade = grade;
    	}
    	public Student() {
    		this("zhangsan",18,100);
    	}
    	@Override
    	public int compare(Student o1, Student o2) {
    		// TODO Auto-generated method stub
    		int num=o1.age-o2.age;
    		if(num==0){
    			num=(int) (o1.grade-o2.grade);
    		}
    		return num;
    	}
    	@Override
    	public String toString() {
    		return "Student [name=" + name + ", age=" + age + ", grade=" + grade + "]";
    	}
    	
    }
    

     

  2. 输出结果

    Student [name=zhangsan, age=18, grade=100.0]
    Student [name=wangwu, age=20, grade=90.0]
    Student [name=lisi, age=17, grade=80.0]
    Student [name=zhangliu, age=18, grade=60.0]
    Student [name=wangba, age=15, grade=70.0]
    排序后===========================
    Student [name=wangba, age=15, grade=70.0]
    Student [name=lisi, age=17, grade=80.0]
    Student [name=zhangliu, age=18, grade=60.0]
    Student [name=zhangsan, age=18, grade=100.0]
    Student [name=wangwu, age=20, grade=90.0]
    

     

  3. 实现Comparable接口排序

    package com.collection;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    /**
     * list排序规则2
     * @author 26920
     *
     */
    public class TestList02 {
    	public static void main(String[] args) {
    		Listlist=new ArrayList();
    		list.add(new Teacher(24,"nan","zhangsan",4500));
    		list.add(new Teacher(25,"nan","wangba",5000));
    		list.add(new Teacher(26,"nan","zhangliu",5500));
    		list.add(new Teacher(27,"nan","lisi",7500));
    		list.add(new Teacher(25,"nan","wangwu",5000));
    		for (Teacher teacher : list) {
    			System.out.println(teacher.toString());
    		}
    		Collections.sort(list);
    		System.out.println("排序后===========================");
    		for (Teacher teacher : list) {
    			System.out.println(teacher.toString());
    		}
    	}
    }
    class Teacher implements Comparable{
    	private int age;
    	private String sex;
    	private String name;
    	private double salary;
    	@Override
    	public int compareTo(Teacher o) {
    		int num=this.age-o.age;
    		if(num==0){
    			num=(int) (this.salary-o.salary);
    		}
    		return num;
    	}
    	public Teacher(int age, String sex, String name, double salary) {
    		super();
    		this.age = age;
    		this.sex = sex;
    		this.name = name;
    		this.salary = salary;
    	}
    	public Teacher() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	@Override
    	public String toString() {
    		return "Teacher [age=" + age + ", sex=" + sex + ", name=" + name + ", salary=" + salary + "]";
    	}
    }

     

  4. 输出结果

    Teacher [age=24, sex=nan, name=zhangsan, salary=4500.0]
    Teacher [age=25, sex=nan, name=wangba, salary=5000.0]
    Teacher [age=26, sex=nan, name=zhangliu, salary=5500.0]
    Teacher [age=27, sex=nan, name=lisi, salary=7500.0]
    Teacher [age=25, sex=nan, name=wangwu, salary=5000.0]
    排序后===========================
    Teacher [age=24, sex=nan, name=zhangsan, salary=4500.0]
    Teacher [age=25, sex=nan, name=wangba, salary=5000.0]
    Teacher [age=25, sex=nan, name=wangwu, salary=5000.0]
    Teacher [age=26, sex=nan, name=zhangliu, salary=5500.0]
    Teacher [age=27, sex=nan, name=lisi, salary=7500.0]
    

     

你可能感兴趣的:(java基础)