学生选课-尝试对学生序列排序Comparable&Comparator简介
代码下载
http://download.csdn.net/download/qq_29132907/10253207
1.Student.java
package com.imooc.studentsSelection;
import java.util.HashSet;
import java.util.Set;
/**
* 学生类
* 实现Comparable接口与compareTo方法,泛型
* @author Administrator
*
*/
public class Student implements Comparable<Student>{
@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;
}
//添加两个属性
public String id;
public String name;
//接口,把属性信息存放在Set属性类型中,set的泛型<course>
public Set<Course> courses;
//构造器-提供id/name可以创建学生
public Student(String id,String name){
this.id=id;
this.name=name;
//Set接口实现类,初始化courses
this.courses=new HashSet<Course>();
}
@Override
public int compareTo(Student o) {
// 当前ID作为比较对象
return this.id.compareTo(o.id);
}
}
2.StudentComparator.java
package com.imooc.studentsSelection;
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);
}
}
3.CollectionsTest.java
package com.imooc.studentsSelection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* 将要完成:
* 1.通过Collections.sort()方法,对Integer泛型的List进行排序;
* 2.对String泛型的List进行排序;
* 3.对其他类型泛型的List进行排序,以Student为例。
* @author Administrator
*
*/
public class CollectionsTest {
/**
* 1.通过Collections.sort()方法,对Integer泛型的List进行排序;
* 创建一个Integer泛型的List,插入十个100以内的不重复随机整数
* 调用Collections.sort()方法对其进行排序
*/
public void testSort1(){
List<Integer> integerList=new ArrayList<Integer>();
//插入十个100以内的不重复随机整数
Random random=new Random();//随机整数
Integer k;
for(int i=0;i<10;i++){
do{
//100以内的随机整数
k=random.nextInt(100);
}while(integerList.contains(k));//不重复整数
integerList.add(k);
System.out.println("成功添加整数:"+k);
}
System.out.println("--------排序前----------");
for(Integer integer:integerList){
System.out.println("元素:"+integer);
}
Collections.sort(integerList);
System.out.println("--------排序后----------");
for(Integer integer:integerList){
System.out.println("元素:"+integer);
}
}
/**
* 2.对字符串String泛型的List进行排序
* 创建String泛型的List,添加三个乱序的String元素,
* 调用sort方法,再次输出排序后的顺序
*/
public void testSort2(){
List<String> stringList=new ArrayList<String>();
stringList.add("microsoft");
stringList.add("google");
stringList.add("lenovo");
System.out.println("--------排序前---------");
for(String string:stringList){
System.out.println("元素:"+string);
}
Collections.sort(stringList);
System.out.println("--------排序后---------");
for(String string:stringList){
System.out.println("元素:"+string);
}
}
/**
* 3.对其他类型泛型的List进行排序,以Student为例
*/
public void testSort3(){
List<Student> studentList=new ArrayList<Student>();
Random random=new Random();//id去1000以内的随机整数
studentList.add(new Student(random.nextInt(1000)+"","小明"));
studentList.add(new Student(random.nextInt(1000)+"","小红"));
studentList.add(new Student(random.nextInt(1000)+"","小兰"));
studentList.add(new Student(10000+"","小兰2"));
System.out.println("--------比较前--------");
for(Student student:studentList){
System.out.println("学生:"+student.id+student.name);
}
Collections.sort(studentList);
System.out.println("-------排序后----------");
for(Student student:studentList){
System.out.println("学生:"+student.id+student.name);
}
Collections.sort(studentList,new StudentComparator());
System.out.println("-------按照姓名排序后----------");
for(Student student:studentList){
System.out.println("学生:"+student.id+student.name);
}
}
/**
* @param args
*/
public static void main(String[] args) {
CollectionsTest ct=new CollectionsTest();
//ct.testSort1();//数字排序
//ct.testSort2();//字符串排序
ct.testSort3();
}
}