[英]Read a CSV file and Sorting it by a field in java(Doubts and explanations)
[英]Sorting csv file by one field
試圖編寫一個使用compareTo對具有5個“字段”的csv文件進行排序的程序,除其他外,我已經獲得了將csv文件導入到arrayList中的代碼。 就是說,我完全停留在實際對填充的arrayList進行排序所需的代碼上。
這是我的代碼:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Student implements Comparable<Student>{
public static final String CSV_PATH = "unsortedList.csv";
public static int studentID, mark;
public static String fName, lName, grade;
public static boolean append = true;
public static ArrayList<String> aList = new ArrayList<String>();
public static void main(String[] args) throws IOException {
readAllLinesFromFile(CSV_PATH);
}
public static ArrayList<String> readAllLinesFromFile(String path) throws IOException{
FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while( (line = bufferedReader.readLine()) != null){
aList.add(line);
}
bufferedReader.close();
System.out.print(aList);
return aList;
}
public static void writeAllLinesToFile(String path, ArrayList<String> aList) throws IOException {
//ArrayList<String> aList = new ArrayList<String>();
FileWriter fileWriter = new FileWriter(path, append);
PrintWriter printWriter = new PrintWriter(fileWriter);
for (String line : aList){
printWriter.printf("%s" + "%n", line);
}
printWriter.close();
}
public Student(int studentID, String fName, String lName, int mark, String grade) {
super();
this.studentID = studentID;
this.fName = fName;
this.lName = lName;
this.mark = mark;
this.grade = grade;
}
public int getStudentID() {
return studentID;
}
public String getfName() {
return fName;
}
public String getlName() {
return lName;
}
public int getMark() {
return mark;
}
public String getGrade() {
return grade;
}
public int compareTo (Student s) {
if (this.mark == s.mark) {
return this.fName.compareTo(s.fName);
} else {
return (s.mark - this.mark) > 0 ? 1 : -1;
}
}
}
這是CSV內容的示例:
StudentID,FirstName,LastName,FinalMark,FinalGrade
123464,John,Guest,77,P
123456,Dianne,Smith,76,P
122364,Stacey,Hobbs,58,P
123472,Laura,Taylor,67,P
123461,Lazarus,Wonton,42,F
123468,Nola,Emirate,50,P
任何幫助是極大的贊賞!
看一下這篇文章: 如何在Java中對ArrayList進行排序
我的看法是,您應該重寫compare函數,然后在arraylist上調用sort函數。
編輯:自己嘗試過。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Main {
public static final String CSV_PATH = "unsortedList.csv";
public static boolean append = true;
public static ArrayList<String> aList = new ArrayList<String>();
public static void main(String[] args) throws IOException {
readAllLinesFromFile(CSV_PATH);
System.out.println("Unsorted:\n");
for(String aStudentString: aList){
System.out.println(aStudentString +"\n");
}
ArrayList<Student> students = convertToStudents(aList);
System.out.println("SORTED:\n");
for(Student student : students){
System.out.println(student.toString());
}
}
public static ArrayList<String> readAllLinesFromFile(String path) throws IOException{
FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while( (line = bufferedReader.readLine()) != null){
aList.add(line);
}
bufferedReader.close();
return aList;
}
public static ArrayList<Student> convertToStudents(ArrayList<String> studentsStrings) {
ArrayList<Student> students = new ArrayList<>();
studentsStrings.remove(0);
for(String studentString : studentsStrings) {
String[] parts = studentString.split(",");
int studentID = Integer.valueOf(parts[0]);
String fName = parts[1];
String lName = parts[2];
int mark = Integer.valueOf(parts[3]);
String grade = parts[4];
students.add(new Student(studentID, fName, lName, mark, grade));
}
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.compareTo(o2);
}
});
return students;
}
public static void writeAllLinesToFile(String path, ArrayList<String> aList) throws IOException {
//ArrayList<String> aList = new ArrayList<String>();
FileWriter fileWriter = new FileWriter(path, append);
PrintWriter printWriter = new PrintWriter(fileWriter);
for (String line : aList){
printWriter.printf("%s" + "%n", line);
}
printWriter.close();
}
}
和學生班:
public class Student implements Comparable<Student>{
public int studentID, mark;
public String fName, lName, grade;
public Student(int studentID, String fName, String lName, int mark, String grade) {
super();
this.studentID = studentID;
this.fName = fName;
this.lName = lName;
this.mark = mark;
this.grade = grade;
}
public int getStudentID() {
return studentID;
}
public String getfName() {
return fName;
}
public String getlName() {
return lName;
}
public int getMark() {
return mark;
}
public String getGrade() {
return grade;
}
@Override
public int compareTo (Student s) {
if (this.mark == s.mark) {
return this.fName.compareTo(s.fName);
} else {
return (s.mark - this.mark) > 0 ? 1 : -1;
}
}
@Override
public String toString() {
return "Student{" + "studentID=" + studentID + ", mark=" + mark + ", fName=" + fName + ", lName=" + lName + ", grade=" + grade + '}';
}
}
您需要為Student
實現Comparable
接口。 根據javadoc, compareTo
需要返回負整數,零或正整數,因為此對象小於,等於或大於指定的對象。 因此,您不必返回-1
或0
或1
:
class Student implements Comparable<Student> {
private int studentID;
private String fName;
private String lName;
private int mark;
private String grade;
public Student(int studentID, String fName, String lName, int mark, String grade) {
this.studentID = studentID;
this.fName = fName;
this.lName = lName;
this.mark = mark;
this.grade = grade;
}
@Override
public int compareTo(Student o) {
return o.mark - this.mark; // DESC
}
@Override
public String toString() {
return studentID + "," + fName + "," + lName + "," + mark + "," + grade;
}
}
您的代碼最終可能看起來像這樣:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
File input = new File("mycvs.cvs");
File output = new File("sorted.cvs");
Main m = new Main();
List<Student> students = m.sort(input, 2);
m.write(output, students);
}
private void write(File output, List<Student> students) throws IOException {
Path path = Paths.get(output.toURI());
List<String> listStudents = new ArrayList<>(students.size());
for (Student s: students) {
listStudents.add(s.toString());
}
Files.write(path, listStudents);
}
private List<Student> sort(File f, int columnToSort) throws IOException {
Path path = Paths.get(f.toURI());
List<String> allLines = Files.readAllLines(path);
List<Student> students = new ArrayList<>(allLines.size());
for (int i = 1; i < allLines.size(); i++) {
String line = allLines.get(i);
String[] columns = line.split(",");
Student s = new Student(Integer.parseInt(columns[0]), columns[1], columns[2], Integer.parseInt(columns[3]), columns[4]);
students.add(s);
}
Collections.sort(students);
return students;
}
}
class Student implements Comparable<Student> {
private int studentID;
private String fName;
private String lName;
private int mark;
private String grade;
public Student(int studentID, String fName, String lName, int mark, String grade) {
this.studentID = studentID;
this.fName = fName;
this.lName = lName;
this.mark = mark;
this.grade = grade;
}
@Override
public int compareTo(Student o) {
return o.mark - this.mark; // DESC
}
@Override
public String toString() {
return studentID + "," + fName + "," + lName + "," + mark + "," + grade;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.