我有這個檔案要處理,我試圖弄清楚如何將第二行作為不同的物件讀取。在行的末尾,最后一項是公共汽車的數量。第二行、第四行等應包含 idBus 和 noOfBus,以逗號分隔。
1,Harvard University,2
1,140,2,56
2,Massachusetts Institute of Technology,2
1,240,5,56
3,University of California Berkeley,3
1,112,2,56,3,28
4,Columbia University,4
1,84,2,84,3,84,7,28
基于這些類
class University {
public int idUniversity;
public String nameOfUniversity;
public int noOfBuses;
}
class Bus {
public int idBus;
public int noOfBus;
}
我想要一個函式來處理 CSV 檔案中所有公共汽車和大學線路的串列。它必須使用 Stream API。我不知道這是否可能或如何進行。
當然,可以根據類進行更改,但保留變數而不添加任何額外內容。
我認為最方便的方法是瀏覽檔案并根據兩個類(大學和公共汽車)獲取兩個串列,最后將它們添加到第三個串列中,然后進行處理,但我不知道如何處理。
基于此檔案,我必須使用 Stream API 進行處理,例如“按 ID 排序巴士”或“哪些大學有更多巴士?” 或“哪輛公共汽車去幾所大學?”。
這就是我一直在嘗試做的。通常這就是我瀏覽 CSV 檔案的方式。
private static List<University> readListOfUniversities() throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(PATH_UNIVERSITIES_TEXT_FILE))) {
return bufferedReader.lines().map(line -> {
String[] tokens = line.split(",");
var idUniversity = Integer.parseInt(tokens[0]);
var nameOfUniversity = tokens[1].trim();
var noOfBuses = Integer.parseInt(tokens[2]);
var university = new University(idUniversity, nameOfUniversity, noOfBuses);
return university;
}).collect(Collectors.toList());
}
}
像這個:
1,Harvard University,2
2,Massachusetts Institute of Technology,2
3,University of California Berkeley,3
4,Columbia University,4
uj5u.com熱心網友回復:
這是我的做法(我對你的課程做了一些修改)
public class University {
private int id;
private String name;
private List<Bus> busList;
//let the constructor do the work
public University(String [] data, List<Bus> busList) {
id = Integer.parseInt(data[0]);
name = data[1];
this.busList = busList;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<Bus> getBusList() {
return busList;
}
@Override
public String toString() {
return "University [id=" id ", name=" name ", busList=" busList "]";
}
}
public class Bus {
private int id;
private int num;
public Bus(int id, int num) {
this.id = id;
this.num = num;
}
public int getId() {
return id;
}
public int getNum() {
return num;
}
//Method to parse the data into usuable objects
public static List<Bus> generateBusInfo(String [] data) {
List<Bus> buses = new ArrayList<>();
for (int i = 0; i < data.length-1; i =2) {
int id = Integer.parseInt(data[i]);
int num = Integer.parseInt(data[i 1]);
buses.add(new Bus(id, num));
}
return buses;
}
@Override
public String toString() {
return "Bus [id=" id ", num=" num "]";
}
}
public static void main(String[] args) throws IOException {
//This is just so I dont have to bother with creating a file.
String input = "1,Harvard University,2\n"
"1,140,2,56\n"
"2,Massachusetts Institute of Technology,2\n"
"1,240,5,56\n"
"3,University of California Berkeley,3\n"
"1,112,2,56,3,28\n"
"4,Columbia University,4\n"
"1,84,2,84,3,84,7,28\n";
List<University> universities = new ArrayList<>();
//replace StringReader with a FileReader to the path of your file.
try(BufferedReader reader = new BufferedReader(new StringReader(input))) {
//read the first line
String line = reader.readLine();
while (line != null) { //keep reading until you're done
String [] uniData = line.split(","); //convert string into an array
line = reader.readLine(); //read the next line
if (line != null) {
String [] busData = line.split(","); //convert
List<Bus> busList = Bus.generateBusInfo(busData); //make busses
universities.add(new University(uniData, busList)); //create university and add it to the list
line = reader.readLine(); //read the next line
}
}
}
//output the list
for (University uni : universities) {
System.out.println(uni);
}
}
您還可以將 Universities 和 buss 讀入Maps 以便于訪問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/463149.html
下一篇:如何知道檔案是否以換行符結尾
