我是靠谱客的博主 刻苦电灯胆,最近开发中收集的这篇文章主要介绍集合转文件(字符流),文本文件中的信息读取到新的集合中(学生年龄降序排列),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

学生类为例:

创建学生类:

import java.util.Objects;
public class Student {
private String name;
private String sex;
private int age;
private
String score;
public Student() {
}
public Student(String name, String sex, int age, String score) {
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", sex='" + sex + ''' +
", age='" + age + ''' +
", score='" + score + ''' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name) &&
Objects.equals(sex, student.sex) &&
Objects.equals(score, student.score);
}
@Override
public int hashCode() {
return Objects.hash(name, sex, age, score);
}
}
创建Demo类代码示例:
package demo01;
import java.io.*;
import java.util.*;
class AgeComparator implements Comparator<Student> {
//比较器 Comparator,按照学生年龄降序(从大到小)
//如果升序就o1.getage()-o2.getAge()(从小到大)
@Override
public int compare(Student o1, Student o2) {
return o2.getAge() - o1.getAge();
}
}
public class Demo {
public static void main(String[] args) throws IOException {
ArrayList<Student> studentArrayList = new ArrayList<>();
Student student1 = new Student("刘德华", "男", 22, "89");
Student student2 = new Student("成龙", "男", 19, "99");
Student student3 = new Student("张三", "男", 18, "99");
Student student4 = new Student("李四", "男", 21, "99");
Student student5 = new Student("王五", "男", 25, "99");
studentArrayList.add(student1);
studentArrayList.add(student2);
studentArrayList.add(student3);
studentArrayList.add(student4);
studentArrayList.add(student5);
Collections.sort(studentArrayList, new AgeComparato());
BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"));
for (Student s : studentArrayList
) {
bw.write(s.getName() + "," + s.getSex() + "," + s.getAge() + "," + s.getScore());
bw.newLine();
bw.flush();
}
System.out.println("写入成功");
BufferedReader br = new BufferedReader(new FileReader("student.txt"));
ArrayList<String> arrayList = new ArrayList<>();
String line;
while ((line = br.readLine()) != null) {
arrayList.add(line);
}
br.close();
for (String s : arrayList
) {
System.out.println(s);
}
}
}

对于文件复制有以下几点需要注意:

1.优先选择字符缓冲流,字符流对于文本复制尤其包含中文汉字会效率很高,缓冲流又能间接增加传输效率;

2.字符流复制文本快的一部分原因是可以选择按行复制(优先于字节流复制文本);

3.比较器(Comparator)接口中的方法

int compare(T obj1, T obj2)  

比较o1和o2这两个对象如果: obj1 > obj2,这个方法返回正整数

obj2 == obj1, 返回0

obj1 < obj2,这个方法返回负整数

 

比较器案例

学生集合按照年龄和成绩排序

class Student {
private String name;
private int age;
private int score;
}
//年龄比较器(从小到大)
class AgeComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
//年龄由大到小排序 :return o2.getAge()-o1.getAge();
}
}
//成绩比较器
class ScoreComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore() - o2.getScore();
//成绩由大到小排序:return o2.getScore()-o1.getScore();
}
}
​
public class Demo04Comparator {
public static void main(String[] args) {
//创建学生对象,添加到集合当中
​
List<Student> students = new ArrayList<>();
​
students.add(new Student("小龙",22,97));
​
students.add(new Student("小明",15,86));
​
students.add(new Student("小花",26,100));
​
//按年龄从小到大对集合的学生对象进行排序
​
Collections.sort(students, new AgeComparator());
System.out.println(students);
​
//按分数从低到高对集合中的学生对象进行排序进行排序
​
Collections.sort(students, new ScoreComparator());
​
System.out.println(students);
​
}
​
}

 

 

 

 

最后

以上就是刻苦电灯胆为你收集整理的集合转文件(字符流),文本文件中的信息读取到新的集合中(学生年龄降序排列)的全部内容,希望文章能够帮你解决集合转文件(字符流),文本文件中的信息读取到新的集合中(学生年龄降序排列)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部