使用 Stream API Java 解析文本文件
Parsing text file with Stream API Java
我有这个文件要处理,我正在尝试弄清楚如何将第二行作为不同的对象来读取。在行的末尾,最后一项是公交车的数量。第二行、第四行等应包含以逗号分隔的 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
以下是我的做法(我对您的 类 进行了轻微修改)
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);
}
}
您还可以将大学和公共汽车读入 Map
以便于访问。
我有这个文件要处理,我正在尝试弄清楚如何将第二行作为不同的对象来读取。在行的末尾,最后一项是公交车的数量。第二行、第四行等应包含以逗号分隔的 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
以下是我的做法(我对您的 类 进行了轻微修改)
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);
}
}
您还可以将大学和公共汽车读入 Map
以便于访问。