Java 编程基础题:io流和集合综合相关的题目不会做,哪位大神来看一下

已知项目根目录下student_info.txt文件中有如下数据:(姓名-年龄-总分)
lucy-28-98
lily-23-97
robt-25-100
wili-15-100
klin-29-93
运用IO技术获取将该文件中的数据分别封装成5个Student(姓名为String类型,年龄为int类型,总分为int类型 )对象存入TreeSet集合中(需要自己定义Student类)
要求:
根据学生的总分进行排序(降序),如果分数相同则比较年龄,年龄较大的排在前面。
按照排序完的顺序将所有信息打印到控制台上:
打印格式如下:
robt-25-100
wili-15-100
lucy-28-98
lily-23-97
klin-29-93

此需求的关键点主要是两个:

1、IO读取文本信息;

2、TreeSet的排序问题。

解决过程如下:

首先通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比较器进行排序,而创建自己的实现比较器。

package test.treeset;
import java.util.Comparator;
public class MyComparator implements Comparator<Student> {
 @Override
 public int compare(Student stu1, Student stu2) {
  if (stu1.getScore() > stu2.getScore()) {
   return -1;
  }else if (stu1.getScore() == stu2.getScore()) {
   if (stu1.getAge() > stu2.getAge()) {
    return -1;
   }else if (stu1.getAge() == stu2.getAge()) {
    return 0;
   }else {
    return 1;
   }
  }else {
   return 1;
  }
 }
}

创建学生Bean类

package test.treeset;
import java.io.Serializable;
public class Student implements Serializable {
 private String name;
 private int age;
 private int score;
 public Student(String name, int age, int score) {
  this.name = name;
  this.age = age;
  this.score = score;
 }
 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;
 }
 public int getScore() {
  return score;
 }
 public void setScore(int score) {
  this.score = score;
 }
 @Override
 public String toString() {
  return name+"-"+age+"-"+score;
 }
}

创建实现类,实现对文件的读取已经将读取内容实例化入Student中,添加入TreeSet中排序,之后顺序打印TreeSet数据。

package test.treeset;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.TreeSet;
/**
 * 已知项目根目录下student_info.txt文件中有如下数据:(姓名-年龄-总分)
 * lucy-28-98
 * lily-23-97
 * robt-25-100
 * wili-15-100
 * klin-29-93搜索
 * 运用IO技术获取将该文件中的数据分别封装成5个Student(姓名为String类型,年龄为int类型,总分为int类型 )对象存入TreeSet集合中(需要自己定义Student类)
 * 要求:
 *  根据学生的总分进行排序(降序),如果分数相同则比较年龄,年龄较大的排在前面。
 *   按照排序完的顺序将所有信息打印到控制台上:
 * 打印格式如下:
 *   robt-25-100
 * wili-15-100
 * lucy-28-98
 * lily-23-97
 * klin-29-93 
 */
public class IoReadTest {
 public static void main(String[] args) {
  TreeSet<Student> set = new TreeSet<Student>(new MyComparator());
  try {
   FileInputStream fis = new FileInputStream(new File("D:\\student_info.txt"));
   InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
   BufferedReader br = new BufferedReader(isr);
   String str = "";
   while ((str = br.readLine()) != null) {
    String[] info = str.split("-"); 
    String name = info[0];
    int age = Integer.parseInt(info[1]);
    int score = Integer.parseInt(info[2]);
    Student student = new Student(name,age,score);
    set.add(student);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  for (Student student : set) {
   System.out.println(student);
  }
 }
}

运行后输出结果

robt-25-100
wili-15-100
lucy-28-98
lily-23-97
klin-29-93

温馨提示:答案为网友推荐,仅供参考
相似回答