您的位置:首页 > 编程语言 > Java开发

【慕课笔记】第六章 JAVA中的集合框架(下) 第8节 实现学生序列排序

2016-03-02 17:46 501 查看
第8节 实现学生序列排序

针对上两节的问题,需要对Student类进行一下改造:

改造一:

首先要实现Comparable接口,

然后实现compareTo()方法

返回值是int,相等则返回0,参数比当前对象小则返回正整数,大则返回负整数

import java.util.HashSet;
import java.util.Set;
/*
* 学生类
*/
public class Student implements Comparable<Student> {
public String id;
public String name;
public Set<Course> courses;
public Student(String id,String name){
this.id=id;
this.name=name;
this.courses=new HashSet<Course>();//Set是接口,实例化要用HashSet类
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Student))
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return this.id.compareTo(o.id);
}

}


public void testSort4(){
List<Student> stuList=new ArrayList<Student>();
Random random=new Random();
stuList.add(new Student(random.nextInt(1000)+"","Mike"));
stuList.add(new Student(random.nextInt(1000)+"","Angela"));
stuList.add(new Student(random.nextInt(1000)+"","Lucy"));
stuList.add(new Student(10000+"","Beyonce"));
System.out.println("--------------排序前---------------");
for (Student student : stuList) {
System.out.println("学生:"+student.id+":"+student.name);
}
Collections.sort(stuList);
System.out.println("--------------排序后---------------");
for (Student student : stuList) {
System.out.println("学生:"+student.id+":"+student.name);
}
}



根据执行结果,因为id是String类型,所以先比较的是第一个字符。



改造二:

首先要实现Comparator接口,

然后实现compare()方法

返回值是int,相等则返回0,参数比当前对象小则返回正整数,大则返回负整数



package com.imooc.collection;

import java.util.Comparator;

public class StudentComparator implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.name.compareTo(o2.name);
}

}


public void testSort5(){
List<Student> stuList=new ArrayList<Student>();
Random random=new Random();
stuList.add(new Student(random.nextInt(1000)+"","Mike"));
stuList.add(new Student(random.nextInt(1000)+"","Angela"));
stuList.add(new Student(random.nextInt(1000)+"","Lucy"));
stuList.add(new Student(10000+"","Beyonce"));
System.out.println("--------------排序前---------------");
for (Student student : stuList) {
System.out.println("学生:"+student.id+":"+student.name);
}
Collections.sort(stuList);
System.out.println("--------------排序后---------------");
for (Student student : stuList) {
System.out.println("学生:"+student.id+":"+student.name);
}
Collections.sort(stuList, new StudentComparator());
System.out.println("--------------按姓名排序后---------------");
for (Student student : stuList) {
System.out.println("学生:"+student.id+":"+student.name);
}
}


执行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: