习题
public class Student2 {
private String name;
private int age;
public Student2() {
}
public Student2(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.util.Comparator;
import java.util.TreeSet;
/*
TreeSet集合存储自定义对象并遍历
如果对象的成员变量值相同即为同一个对象
按照年龄进行从大到小进行排序
*/
public class TreeSetDemo4 {
public static void main(String[] args) {
//今后推荐使用匿名内部类的形式实现比较器排序
TreeSet<Student2> student2s = new TreeSet<Student2>(new Comparator<Student2>() {
@Override
public int compare(Student2 o1, Student2 o2) {
// 按照年龄进行从大到小进行排序
int i = o2.getAge() - o1.getAge();
//比较隐含条件,年龄一样,姓名不一定一样
int i2 = i == 0 ? o1.getName().compareTo(o2.getName()) : i;
return i2;
}
});
//创建学生对象
Student2 s1 = new Student2("小朱", 18);
Student2 s2 = new Student2("小刘", 18);
Student2 s3 = new Student2("小龙", 16);
Student2 s4 = new Student2("小可爱", 19);
//将学生对象添加到集合中
student2s.add(s1);
student2s.add(s2);
student2s.add(s3);
student2s.add(s4);
for(Student2 s : student2s){
System.out.println(s);
}
}
}
import java.util.Random;
import java.util.TreeSet;
/*
编写一个程序,获取10个1至20的随机数,要求随机数不能重复。
*/
public class TreeSetDemo5 {
public static void main(String[] args) {
//创建随机数对象
Random random = new Random();
//使用自然排序--无参构造即可,因为Integer类型实现了Comparable接口
TreeSet<Integer> integers = new TreeSet<>();
while (true){
if(integers.size()==10){
break;
}
//生成随机数
int number = random.nextInt(20) + 1;
integers.add(number);
}
System.out.println(integers);
}
}
public class Student3 {
private String name;
//学科
private int chinese;
private int math;
private int english;
public Student3() {
}
public Student3(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSum(){
return chinese + math + english;
}
@Override
public String toString() {
return "Student3{" +
"name='" + name + '\'' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}';
}
}
import java.util.Comparator;
import java.util.TreeSet;
/*
键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),
按照总分从高到低输出到控制台,假设班级没有重名的。
*/
import java.util.Scanner;
public class TreeSetDemo6 {
public static void main(String[] args) {
//创建一个TreeSet集合对象,匿名内部类的形式实现Comparator接口
TreeSet<Student3> ts = new TreeSet<>(new Comparator<Student3>() {
@Override
public int compare(Student3 o1, Student3 o2) {
//按照总分从高到低
int i = o2.getSum() - o1.getSum();
//总分一样,语文成绩不一定一样
int i2 = i == 0 ? o1.getChinese() - o2.getChinese() : i;
//总分一样,语文成绩一样,数学成绩不一定一样
int i3 = i2 == 0 ? o1.getMath() - o2.getMath() : i2;
//总分一样,语文成绩一样,数学成绩一样,英语成绩不一定一样
int i4 = i3 == 0 ? o1.getEnglish() - o2.getEnglish() : i3;
//总分一样,语文成绩一样,数学成绩一样,英语成绩一样,姓名不一定一样
int i5 = i4 == 0 ? o1.getName().compareTo(o2.getName()) : i4;
return i5;
}
});
//创建Scanner对象
Scanner sc = new Scanner(System.in);
for (int i = 1; i < 6; i++) {
System.out.println("请输入第" + i + "个学生的姓名:");
String name = sc.next();
System.out.println("请输入" + name + "的语文成绩:");
int chinese = sc.nextInt();
System.out.println("请输入" + name + "的数学成绩:");
int math = sc.nextInt();
System.out.println("请输入" + name + "的英语成绩:");
int english = sc.nextInt();
//创建学生对象,将输入的值赋给学生对象的成员变量
Student3 student = new Student3();
student.setName(name);
student.setChinese(chinese);
student.setMath(math);
student.setEnglish(english);
//将学生对象添加到集合中
ts.add(student);
if (i == 5) {
System.out.println("所有的学生信息录入完毕!");
}
}
System.out.println("===========================================");
System.out.println("学生信息如下:");
System.out.println("姓名\t语文成绩\t数学成绩\t英语成绩\t总成绩");
//遍历学生信息
for (Student3 student3 : ts) {
System.out.println(student3.getName() + "\t" + student3.getChinese() + "\t"
+ student3.getMath() + "\t" + student3.getEnglish() + "\t" + student3.getSum());
}
}
}