【发布时间】:2020-06-09 06:19:40
【问题描述】:
我的问题是我需要计算每个 ID 的平均时间并计算每个 ID 的平均时间。
样本数据
T1,2020-01-16,11:16pm,start
T2,2020-01-16,11:18pm,start
T1,2020-01-16,11:20pm,end
T2,2020-01-16,11:23pm,end
我编写了一个代码,将第一列和第三列保留在地图中......类似于
T1, 11:16pm
但在将这些值保存在地图中后,我无法计算值。还尝试将它们保存在字符串数组中并逐行拆分。该方法也面临同样的问题。 ** 公共类 AverageTimeGenerate {
public static void main(String[] args) throws IOException {
File file = new File("/abc.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
ArrayList<String> list = new ArrayList<>();
String[] tokens = line.split(",");
for (String s: tokens) {
list.add(s);
}
Map<String, String> map = new HashMap<>();
String[] data = line.split(",");
String ids= data[0];
String dates = data[1];
String transactionTime = data[2];
String transactionStartAndEndTime = data[3];
String[] transactionIds = ids.split("/n");
String[] timeOfEachTransaction = transactionTime.split("/n");
for(String id : transactionIds) {
for(String time : timeOfEachTransaction) {
map.put(id, time);
}
}
}
}
}
}
Can anyone suggest me is it possible to find duplicates in a map and compute values in map, Or is there any other way I can do this so that the output should be like
`T1 2:00
T2 5:00'
【问题讨论】:
-
您对 avg 的预期结果不清楚。请根据您的样本数据说明 T1 2.00 和 T2 5.00 是如何到来的。
-
不,我只是给出了数字来理解。此数据不会获得这些值。它将来 T1 和 T1 的平均时间我的意思是结束时间 - 开始时间/总数。交易次数
-
你可以将它存储到 Map
>> 然后对象看起来像这样 { 'T1'.: {'start' : [11:00, 12:00 ...],'结束':[11:30, 12:30...]},'T2' .. }。我认为计算平均值会更容易,因为您需要区分“开始”和“结束”时间
标签: java arrays string java-8 dynamic-arrays