JAVA集合四:比较器--类自定义排序

参考链接:

HOW2J.CN

前言

对于JAVA集合,都能够用集合的工具类Collections 提供的方法:

  1. Collections.sort(List list)
  2. Collections.sort(List list, Comparator c)

来进行排序。很多时候,集合中存储的不是基本的数据类型,而是我们自己定义的类的对象,我们希望用自己的排序方式对集合中的元素进行排序,我们可以通过让类实现Comparable接口来自定义排序方式,也可以采用创建匿名内部类的做法进行自定义排序

实现Comparable接口

  1. 让待排序的类继承Comparable接口,并实现compareTo方法(相当于给类提供了排序依据)

    package blog;
    
    /*
     * Student 类
     */
    public class Student implements Comparable<Student>{
    	private String name;
    	private int id;
    	public Student(String name, int id) {
    		this.name = name;
    		this.id = id;
    	}
    	@Override
    	public String toString() {
    		return "Student [name=" + name + ", id=" + id + "]";
    	}
    	//实现对象比较的方法(id从小到大排序)
    	@Override
    	public int compareTo(Student o) {
    		if(id < o.id)
    			return -1;
    		else
    		 return 1;
    	}
    }
    
  2. 直接使用Collections.sort(List list)进行排序

    package blog;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
        	List<Student> students = new ArrayList<Student>();
        	students.add(new Student("xsy", 2));
        	students.add(new Student("theory", 1));
        	students.add(new Student("衍射",3));
        	
        	//排序前
        	System.out.println("排序前:");
        	for(Student student : students) {
        		System.out.println(student);
        	}
        	
        	//排序后(按照id从小到大)
        	Collections.sort(students);
        	System.out.println("排序后:");
        	for(Student student : students) {
        		System.out.println(student);
        	}
        }
    }
    

    排序前:
    Student [name=xsy, id=2]
    Student [name=theory, id=1]
    Student [name=衍射, id=3]
    排序后:
    Student [name=theory, id=1]
    Student [name=xsy, id=2]
    Student [name=衍射, id=3]

JAVA创建匿名内部类

类不需要继承接口,只需要在排序时重写Comparator的compare方法:

package blog;

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

public class Main {
    public static void main(String[] args) {
    	List<Student> students = new ArrayList<Student>();
    	students.add(new Student("xsy", 2));
    	students.add(new Student("theory", 1));
    	students.add(new Student("衍射",3));
    	
    	//排序前
    	System.out.println("排序前:");
    	for(Student student : students) {
    		System.out.println(student);
    	}
    	
    	//排序后(按照id从小到大)-- 重写Comparator的compare方法
    	Collections.sort(students, new Comparator() {
			@Override
			public int compare(Object o1, Object o2) {
				if(o1 instanceof Student && o2 instanceof Student) {
					Student s1 = (Student)o1;
					Student s2 = (Student)o2;
					return s1.getId()-s2.getId();
				}
				return 0;
			}		
		});
    	System.out.println("排序后:");
    	for(Student student : students) {
    		System.out.println(student);
    	}
    }
}

运行结果如下:

排序前:
Student [name=xsy, id=2]
Student [name=theory, id=1]
Student [name=衍射, id=3]
排序后:
Student [name=theory, id=1]
Student [name=xsy, id=2]
Student [name=衍射, id=3]

posted @ 2019-10-17 19:08  衍射  阅读(318)  评论(0编辑  收藏  举报