【发布时间】:2017-03-23 12:11:22
【问题描述】:
我正在使用我的“CsvReader”读取一个 csv 文件,如下所示:
public class CsvReader {
public static void main(String[] args) {
Map<String,Employee> empl = new HashMap <String,Employee>();
String csvFile= "C:/Users/rapurohi/Desktop/employee.csv";
BufferedReader br=null;
String line= "";
String csvSplitBy=",";
try{
br=new BufferedReader(new FileReader(csvFile));
while ((line =br.readLine())!=null){
String [] employee=line.split(csvSplitBy);
Employee e=new Employee(employee[0], employee[1], employee[2]);
empl.put((employee[0]),e);
System.out.println("Employee[id="+employee[0]+"name="+employee[1]+"salary="+employee[2]+"]" );
}
}catch (FileNotFoundException|IOException e){
e.printStackTrace();
}
}finally{
if(br!=null){
try{
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
我使用的 csv 如下所示:
empid,name,salary
"456","John","4000"
"457","James","5000"
"458","Jack","3000"
"459","Jenny","6000"
员工的数据VO看起来像
包 com.cg.dto;
公共类员工{
字符串 empId;
字符串 empName;
字符串雇员工资;
公共字符串 getEmpId() {
返回 empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
公共字符串 getEmpName() {
返回员工姓名;
}
public void setEmpName(String empName) {
this.empName = empName;
}
公共字符串 getEmpSalary() {
返回雇员工资;
}
public void setEmpSalary(String empSalary) {
this.empSalary = empSalary;
}
public Employee(String empId, String empName, String empSalary) {
超级();
this.empId = empId;
this.empName = empName;
this.empSalary = empSalary;
}
我需要找到最高薪水并将该条目写入 Csv 文件作为输出。
【问题讨论】:
-
发生了哪些意想不到的事情?
-
我被困在这个问题上。我应该如何继续它允许我获取薪水列并在单独的 csv 中给出最高薪水。
-
@Choirbean 集合和迭代器由于 Hashmap 中的员工对象而不起作用。
-
empl.values()应该返回一个Collection<Employee>。 -
实际上collections.max(empl.values()) 不适用于员工类型