Set to file (character stream), text file information read into the new set (descending order of student age)

Examples of student classes are as follows:

Create student classes:

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);
    }
}
Create Demo class code examples:
 
package demo01;

import java.io.*;
import java.util.*;

class AgeComparator implements Comparator<Student> {
    //Comparator, in descending order according to student's age (from big to small)
    //If the ascending order is o1.getage()-o2.getAge() (from small to large)
    @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("Lau Andy", "male", 22, "89");
        Student student2 = new Student("Jackie Chan", "male", 19, "99");
        Student student3 = new Student("Zhang San", "male", 18, "99");
        Student student4 = new Student("Li Si", "male", 21, "99");
        Student student5 = new Student("Wang Wu", "male", 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("Write successfully");
        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);
        }
    }
}

The following points should be noted for file replication:

1., the character buffer stream is preferred. Character stream is very efficient for text reproduction, especially Chinese characters, and buffer flow can indirectly increase the transmission efficiency.

2. Part of the reason why character stream can copy text quickly is that you can choose to copy by line (prior to byte stream);

3. Methods in Comparator Interface

int compare(T obj1, T obj2)  

Compare o1 with o2. If obj1 > obj2, this method returns a positive integer.

obj2 == obj1, returns 0

Obj1 < obj2, which returns negative integers

 

Comparator case

The student set is ranked by age and achievement.

class Student {
    private String name;
    private int age;
    private int score;
}
//Age comparator (from childhood to age)
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
//Age ranged from older to younger: return o2.getAge()-o1.getAge();
    }
}
//Achievement comparator
class ScoreComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getScore() - o2.getScore();
//The results were sorted from big to small: return o2.getScore()-o1.getScore();
    }
}
​
public class Demo04Comparator {
    public static void main(String[] args) {

        //Create student objects and add them to collections
​
        List<Student> students = new ArrayList<>();
​
        students.add(new Student("Dragons",22,97));
​
        students.add(new Student("Xiao Ming",15,86));
​
        students.add(new Student("Floret",26,100));
​
       
        //Sort the set of student objects by age from small to large
​
        Collections.sort(students, new AgeComparator());

        System.out.println(students);
​
        //Sort the student objects in the set according to the scores from low to high
​
        Collections.sort(students, new ScoreComparator());
​
        System.out.println(students);
​
    }
​
}

 

 

 

 

Keywords: Java

Added by cafegirl on Thu, 03 Oct 2019 03:59:16 +0300