[英]Skip header of CSV file while reading in core Java with below approach
I want to skip the header from this csv file with the below reading mode approach. 我想使用以下阅读模式方法从此csv文件中跳过标题。 Restricting to this mode because I am using Collections.sort which uses List as an argument.
由于我使用的是使用List作为参数的Collections.sort,因此仅限于此模式。
Scenario : I am holding around 20 columns and I need to sort on the 3rd fields, then need to take the average based on 18th col for each key(3rd field). 场景:我持有约20列,我需要在第三个字段上进行排序,然后需要根据每个键(第三个字段)的第18个列取平均值。 In output only 4 columns is required.
在输出中仅需要4列。
------------ skip -------
File file = new File("C:\\Users\\shekhar\\Desktop\\Lab\\DumpData.csv");
//Declare file
List<String> lines =
Files.readAllLines(file.toPath(),StandardCharsets.UTF_8);//Reading file
Collections.sort(lines,new EmpIdComparator1()); // Sorting on 3rd field using comparator
Map<String,List<String>> employeeById = new HashMap<>(); // Map used to decrease the column
------------ skip ----------------------------------
Input Data is given below in comma separated value format: 下面以逗号分隔值格式提供输入数据:
Employee Name,Employee ID,Working Hours,Date,<more columns >
===========================================
Sam,002,6.5,1 Nov
Sam,002,8,2 Nov
为什么不只是在您的collections.sort之前通过遍历原始列表创建新的List数组,而是使用变量if(lineno> 2){.....为list array添加代码}忽略第一行,然后执行排序在这个新阵列上,而不是原来的阵列上。
我会说基于您的员工编号使用比较器,因此排序仅在员工编号000以上的值上发生。
Sublist does what you want, I think. 我认为,子列表可以满足您的需求。
Collections.sort(lines.sublist(1, lines.size()),new EmpIdComparator1());
It only sorts the part, or view, of the list that you are passing into the method. 它仅对要传递给方法的列表的一部分或视图进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.