Java基础之比较器 Comparable和Comparator

本文介绍了一个使用Java实现的学生对象排序示例。通过自定义比较器Comparator和实现Comparable接口的方法,根据学生的分数和年龄进行排序。示例展示了如何利用Collections.sort()和Arrays.sort()方法对学生列表和数组进行排序。
package com.git.base.comparable;

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

/**
 * 比较器的使用演示:
 * 规则 比较分数 大的在前面 分数相同比较年龄,年龄小的在前面
 * 年龄相同,返回相同
 * <p>Title: ComparableDemo.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: Sage</p>
 * @author 五虎将
 * @date 2016年5月18日下午9:23:53
 * @version 1.0
 */
public class ComparableDemo {

	public static void main(String[] args) {
		
		ArrayList<Student> list = new ArrayList<Student>();
		
		list.add(new Student("王虎", 14, 80));
		list.add(new Student("雷虎", 12, 90));
		list.add(new Student("风虎", 15, 70));
		list.add(new Student("虎", 11, 100));
		list.add(new Student("赵虎", 13, 80));
		
		Collections.sort(list,new ComparatorStudent());
		
		for (Student student : list) {
			
			System.out.println(student);
			
		}
		
		Student[] stu  = {
				new Student("王虎", 14, 80),
				new Student("雷虎", 12, 90),
				new Student("风虎", 15, 70),
				new Student("宋庆虎", 11, 100),
				new Student("赵虎", 13, 80)
		};
		
		Arrays.sort(stu);
		
		for (Student student : stu) {
			//System.err.println(student);
		}
		
		
	}
	
	
}


package com.git.base.comparable;

import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

public class Student implements Comparable<Student>{

	public Student(String name,int age,int score) {
		this.name = name;
		this.age = age;
		this.score= score;
	}
	
	private String name;
	
	private int age;

	private int score;
	
	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 int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	@Override
	public int compareTo(Student o) {
		
		//比较的逻辑定义
		if(this.getScore()>o.getScore()){
			return -1; //成绩大的在前面
		}else if (this.getScore()<o.getScore()) {
			return 1;
		}
		//都不是 分数相等 比较年龄
		if(this.getAge()<o.getAge()){
			return -1;
		}else if (this.getAge()>o.getAge()) {
			return 1;
		}
		return 0;
	}

	@Override
	public String toString() {
		
		return name +" " + age + " " + score;
	}

	@Override
	public boolean equals(Object obj) {
		if(this ==obj){
			return true;
		}
		if(obj instanceof Student){
			Student s = (Student)obj;
			if(this.getName().equals(s.getName())&&this.getAge()==s.getAge()&&this.getScore()==s.getScore()){
				return true;
			}
		}
		
		return false;
		
		
	}
	
	
	
	
}

package com.git.base.comparable;

import java.util.Comparator;
/**
 * 比较学生
 * <p>Title: ComparatorStudent.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: Sage</p>
 * @author 五虎将
 * @date 2016年5月19日上午12:01:16
 * @version 1.0
 */
public class ComparatorStudent implements Comparator<Student>{

	@Override
	public int compare(Student o1, Student o2) {
		
		if(o1.equals(o2)){
			return 0;
		}else if (o1.getScore() > o2.getScore()) {
			return -1;
		}else if (o1.getScore() < o2.getScore()) {
			return 1;
		}else{
			if(o1.getAge()<o2.getAge()){
				return -1;
			}else if (o1.getAge() > o2.getAge()) {
				return 1;
			}
		}
		return 0;
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值