【发布时间】:2021-01-07 07:13:31
【问题描述】:
这是我的代码:
public static Map<String, List<Customer>> readCustomerData() throws IOException {
Map<String, List<Customer>> customers =
Files.lines(Paths.get("customer.csv"))
.map(line -> line.split("\\s*,\\s*"))
.map(field -> new Customer(
Integer.parseInt(field[0]), field[1],
Integer.parseInt(field[2]), field[3]))
.collect(Collectors
.groupingBy(Customer::getName));
System.out.println (customers);
return customers;
}
我注意到这段代码将我在 csv 文件中的数据读取到一个元素中,如下所示:
(Ali = ["1 Ali 1201345673 Normal"] , Siti = ["2 Siti 1307891435 Normal"])
但在我的想法中,我想读取数组列表之类的数据,例如对于 Ali:1 是一个元素,Ali 是一个元素,1201345673 是一个元素,Normal 是 Map 客户列表中的另一个元素.我怎样才能修改我的代码来做这样的事情?
这是我的客户类,以防万一:
public class Customer {
private int customerNo;
private String name;
private int phoneNo;
private String status;
public Customer () {}
public Customer (int customerNo, String name, int phoneNo, String status){
this.customerNo = customerNo;
this.name = name;
this.phoneNo = phoneNo;
this.status = status;
}
public String getName(){
return name;
}
public String toString(){
return customerNo + " " + name + " " + phoneNo + " " + status;
}
这是我的 csv 文件:
1,Ali,1201345673,Normal
2,Siti,1307891435,Normal
感谢您的关注。
【问题讨论】: