比较java

时间:2017-03-23 12:11:22

标签: sorting csv

我正在使用我的' 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"

员工的dataVO看起来像

包com.cg.dto;

公共类员工{

String empId;

String empName;

String empSalary;

public String getEmpId(){

return empId;

}

public void setEmpId(String empId){

this.empId = empId;

}

public String getEmpName(){

return empName;

}

public void setEmpName(String empName){

this.empName = empName;

}

public String getEmpSalary(){

return empSalary;

}

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文件作为输出。

1 个答案:

答案 0 :(得分:1)

假设您只需要帮助找到最高工资,这里有一个简单的例子:

int maxSal = 0;
Employee highestPayedEmployee = null;
for (Employee employee : empl.values()) {
    if(employee.getSalary() > maxSal){
        maxSal = employee.getSalary();
        highestPayedEmployee = employee;
    }
}

这假设您的Employee类有一个getSalary()方法,该方法将薪水作为数字返回。

如果需要,您可以将字符串转换为数字:

Integer.parseInt(salaryString);