Java 常用类库 之 比较接口 Comparator

本文深入探讨了Java中的Comparator接口,讲解了如何实现自定义比较规则,并通过实例展示了其在排序和集合操作中的应用。理解Comparator接口对于优化数据结构和算法至关重要。
摘要由CSDN通过智能技术生成

http://www.verejava.com/?id=169931036202101

/**
	知识点: 比较类 Comparator

	题目: 将某班学生按数学成绩从小到大排序

	思路:
		1. 抽象出类:
			1.1 班级(ClassSet)
			1.2 学生(Student)
		2. 找出类关系:
			2.1 学生 属于 班级 Student -> ClassSet(多对1)
		3. 找出类属性:
			3.1 ClassSet(班级名称,班级人数)
			3.2 Student(学生名称,数学成绩)
		4. 找出类方法:
			4.1 学生添加到班级 ClassSet{addStudent(Student s)}
			4.2 学生成绩从小到大排序  ClassSet{sortByScore()}
*/
import java.util.Arrays;
import java.util.Comparator;
public class TestComparator
{
	public static void main(String[] args)
	{
		//实例化4G班级
		ClassSet c=new ClassSet("4G",4);
		//添加学生
		c.addStudent(new Student("李明",90));
		c.addStudent(new Student("李浩",80));
		c.addStudent(new Student("王涛",95));
		c.addStudent(new Student("张胜",70));

		//获得4G班级学生数组集合
		Student[] students=c.getStudents();
		//输出学生信息
		for(Student s:students)
		{
			if(s!=null)
				System.out.println(s.getName()+","+s.getMathScore());
		}

		System.out.println("\n根据学生成绩升序排序");
		Arrays.sort(students,new StudentAscComparator());
		for(Student s:students)
		{
			if(s!=null)
				System.out.println(s.getName()+","+s.getMathScore());
		}

		System.out.println("\n根据学生成绩降序排序");
		Arrays.sort(students,new StudentDescComparator());
		for(Student s:students)
		{
			if(s!=null)
				System.out.println(s.getName()+","+s.getMathScore());
		}

	}
}
class ClassSet
{
	private String className;//班级名称
	private int maxSize;//班级学生人数
	private int currentSize;//当前多少学生
	private Student[] students;//所有学生的数组

	public ClassSet(String className,int maxSize)
	{
		this.className=className;
		this.maxSize=maxSize;
		students=new Student[maxSize];
	}

	public Student[] getStudents()
	{
		return this.students;
	}

	/**
		添加学生
	*/
	public void addStudent(Student s)
	{
		for(int i=0;i<students.length;i++)
		{
			if(students[i]==null)
			{
				students[i]=s;
				currentSize++;
				break;
			}
		}
	}
	
}
class Student 
{
	private String name;//学生姓名
	private int mathScore;//数学成绩

	public Student(String name,int mathScore)
	{
		this.name=name;
		this.mathScore=mathScore;
	}
	public String getName()
	{
		return this.name;
	}
	public void setName(String name)
	{
		this.name=name;
	}
	public int getMathScore()
	{
		return this.mathScore;
	}
	public void setMathScore(int mathScore)
	{
		this.mathScore=mathScore;
	}
}
/**
	学生升序排列
*/
class StudentAscComparator implements Comparator
{
	public int compare(Object o1,Object o2)
	{
		if((o1 instanceof Student)&&(o2 instanceof Student))
		{	
			Student s1=(Student)o1;
			Student s2=(Student)o2;
			if(s1.getMathScore()>s2.getMathScore())
				return 1;
			if(s1.getMathScore()<s2.getMathScore())
				return -1;
		}
		return 0;
	}
}
/**
	学生降序排列
*/
class StudentDescComparator implements Comparator
{
	public int compare(Object o1,Object o2)
	{
		if((o1 instanceof Student)&&(o2 instanceof Student))
		{	
			Student s1=(Student)o1;
			Student s2=(Student)o2;
			if(s1.getMathScore()>s2.getMathScore())
				return -1;
			if(s1.getMathScore()<s2.getMathScore())
				return 1;
		}
		return 0;
	}
}

http://www.verejava.com/?id=169931036202101

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值