Java:读取CSV文件以获取各种统计数据的最小值、最大值、平均值
我想编写一个 Java 类,它将获取 CSV 文件并获取各种统计数据的最小值、最大值、平均值等。请参阅下面的该文件的示例。例如,我想获取“打开登录页面”、“作为主要用户登录”等的统计信息。
我首先将 TestName 和 Time 读取到地图中:
static Map<String, String> data = new TreeMap<String, String>();
...
for (String line : file) {
if (line.contains("Test Name")){
//Get next line, this is the first one.
continue;
}
String TestName = (line.substring(0,line.indexOf(","))).trim();
String Time = (line.substring(line.lastIndexOf(",")+2, line.length())).trim();
//System.out.println(TestName + " " + Time);
data.put(TestName, Time);
}
这很好用。但是,我不确定获取所有唯一值并运行计算的最佳方法。我不需要使用地图,这就是我脑海中突然出现的东西。有谁知道如何做这样的事情?
CSV 文件示例:
Test Name, Thread No, Run No, Time(s)
Opening login page, 0, 0, 1.8869998455047607
Opening login page, 1, 0, 2.246999979019165
Opening login page, 2, 0, 2.1710000038146973
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as lead user, 0, 0, 23.616000175476074
Opening login page, 13, 0, 2.125999927520752
Opening login page, 15, 0, 1.8939998149871826
Logging in as lead user, 3, 0, 20.244999885559082
Logging in as lead user, 2, 0, 23.039999961853027
I want to write a Java class that will take a CSV file and take the min, max, average, etc... of various statistics. Please see below for a sample of this file. For example, I want to get the stats for "Opening login page", "Logging in as lead user", etc..
I have started by reading TestName and Time into a Map:
static Map<String, String> data = new TreeMap<String, String>();
...
for (String line : file) {
if (line.contains("Test Name")){
//Get next line, this is the first one.
continue;
}
String TestName = (line.substring(0,line.indexOf(","))).trim();
String Time = (line.substring(line.lastIndexOf(",")+2, line.length())).trim();
//System.out.println(TestName + " " + Time);
data.put(TestName, Time);
}
This works fine. However, I'm not sure of the best way to go about getting all unique values and running the calculation. I don't need to use a Map, it was what just popped into my head. Does anyone know how to do something like this?
CSV file sample:
Test Name, Thread No, Run No, Time(s)
Opening login page, 0, 0, 1.8869998455047607
Opening login page, 1, 0, 2.246999979019165
Opening login page, 2, 0, 2.1710000038146973
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as regular user, 1, 0, 22.009999990463257
Logging in as lead user, 0, 0, 23.616000175476074
Opening login page, 13, 0, 2.125999927520752
Opening login page, 15, 0, 1.8939998149871826
Logging in as lead user, 3, 0, 20.244999885559082
Logging in as lead user, 2, 0, 23.039999961853027
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过使用某些 CSV JDBC 驱动程序(例如 this 或 这个。加载此类驱动程序后,您可以使用 ANSI SQL 执行所需的任何类型的查询。
You can make use of SQL on top of a CSV file by using some CSV JDBC driver like this one or this one. Once you have loaded such a driver, you can perform whatever type of query you need by using ANSI SQL.
对于解析csv,请参阅
http://www.java-examples.com/parse-csv -file-using-stringtokenizer-example
您可以使用 Set 因为您需要唯一的值。
对于统计数据,请使用 Collections 类的函数,例如 Collections.max(hashSetObj)。
For parsing csv, refer
http://www.java-examples.com/parse-csv-file-using-stringtokenizer-example
You can use Set as u need unique values.
For stats, use functions of Collections class like Collections.max(hashSetObj).
有两件事:
Two things: