114-汉字比较器
9 public class Test03 { 10 public static void main(String[] args) { 11 //1)定义List集合存储Student对象, 通过泛型指定存储元素类型为Student 12 List<Student> list = new ArrayList<>(); 13 14 //2) 向集合中添加元素19 list.add(new Student("wangwu", 26, 78)); 20 list.add(new Student("zhaoliu", 23, 78.5)); 21 list.add(new Student("chenqi", 27, 78.8)); 22 list.add(new Student("zhangsan", 21, 86)); 23 list.add(new Student("feifei", 26, 54));41 42 //6) 对集合中学生对象根据年龄升序排序 43 list.sort(new Comparator<Student>() { 44 @Override 45 public int compare(Student o1, Student o2) { 46 //o1的年龄大返回正数 47 return o1.getAge() - o2.getAge(); 48 } 49 });61 62 //8)根据姓名升序排序 63 list.add(new Student("陈飞", 18, 99)); 64 list.add(new Student("张晓三", 18, 29)); 65 list.add(new Student("李大四", 68, 79)); 66 list.sort(new Comparator<Student>() { 67 @Override 68 public int compare(Student o1, Student o2) { 69 return o1.getName().compareTo(o2.getName()); 70 //compareTo()方法比较的是字符的码值 71 } 72 }); 73 printInfo(list); 74 75 //9) 根据姓名升序排序, 根据汉字顺序比较 76 //先获得一个汉字的比较器 77 Comparator cmp = Collator.getInstance(Locale.CHINESE); 78 list.sort(new Comparator<Student>() { 79 @Override 80 public int compare(Student o1, Student o2) { 81 return cmp.compare(o1.getName(), o2.getName()); 82 } 83 }); 84 printInfo(list); 85 86 }94 }
package list;
import java.text.Collator;
import java.util.*;
/**
* List集合存储Student对象
*/
public class Test03 {
public static void main(String[] args) {
//1)定义List集合存储Student对象, 通过泛型指定存储元素类型为Student
List<Student> list = new ArrayList<>();
//2) 向集合中添加元素
Student stu = new Student();
list.add(stu);
Student stu2 = new Student("lisi", 22, 98);
list.add(stu2);
list.add(new Student("wangwu", 26, 78));
list.add(new Student("zhaoliu", 23, 78.5));
list.add(new Student("chenqi", 27, 78.8));
list.add(new Student("zhangsan", 21, 86));
list.add(new Student("feifei", 26, 54));
//3)调用方法打印List集合
printInfo( list );
//4) 判断集合中是否包含指定的Student对象, list接口引用了ArrayList对象, 通过list调用contains()方法实际执行的是ArrayList对象的contains()方法, 这是接口多态. 在ArrayList的contains()源码中,判断两个元素是否一样调用了equals()方法, 当前ArrayList存储的Student对象, 需要在Student类中重写equals()方法
System.out.println( list.contains( stu ) ); //true
stu = new Student("zhangsan", 21, 86);
System.out.println( list.contains(stu)); //true
//5)把list集合中成绩小于70分的同学都删除. 迭代删除
for (Iterator<Student> iterator = list.iterator(); iterator.hasNext(); ) {
Student student = iterator.next();
if ( student.getScore() < 70 ){
iterator.remove();
}
}
printInfo(list);
//6) 对集合中学生对象根据年龄升序排序
list.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//o1的年龄大返回正数
return o1.getAge() - o2.getAge();
}
});
printInfo(list);
//7)对集合中的元素根据成绩降序排序
list.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//o2的成绩大返回正数
return Double.compare(o2.getScore() , o1.getScore());
}
});
printInfo(list);
//8)根据姓名升序排序
list.add(new Student("陈飞", 18, 99));
list.add(new Student("张晓三", 18, 29));
list.add(new Student("李大四", 68, 79));
list.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
//compareTo()方法比较的是字符的码值
}
});
printInfo(list);
//9) 根据姓名升序排序, 根据汉字顺序比较
//先获得一个汉字的比较器
Comparator cmp = Collator.getInstance(Locale.CHINESE);
list.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return cmp.compare(o1.getName(), o2.getName());
}
});
printInfo(list);
}
private static void printInfo(List<Student> list) {
System.out.println("-------学生信息-----------");
for (Student student : list) {
System.out.println( student);
}
}
}