Java:读取CSV文件以获取各种统计数据的最小值、最大值、平均值

发布于 2024-10-20 05:20:38 字数 1269 浏览 1 评论 0原文

我想编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-10-27 05:20:38

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.

捂风挽笑 2024-10-27 05:20:38

对于解析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).

煞人兵器 2024-10-27 05:20:38

有两件事:

  1. 要计算短语的频率,您可以使用 哈希表,使用短语作为键,每次遇到短语时加一。
  2. 使用 CSV 解析器可以让您的生活变得更轻松。

Two things:

  1. For counting the frequency of phrases you could use a HashTable that uses the phrase as the key and increments by one each time the phrase is encountered.
  2. You could make your life a little easier by using a CSV parser library.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文