Java:从多个数组中的重复项中获取平均时间

时间:2012-10-03 19:43:28

标签: java arrays

我正在编写一个Java类来解析一个类似于以下内容的文件:

[thread 16] INFO - L3: createOrder: min [239.0] max [1245.0] average [488.06]  
[thread 16] INFO - L3: translateBarCode: min [9.0] max [132.0] average [31.1]  
[thread 11] INFO - L3: createOrder: min [258.0] max [2458.0] average [506.31]  
[thread 13] INFO - L3: createOrder: min [243.0] max [1303.0] average [542.57]  
[thread 11] INFO - L3: translateBarCode: min [9.0] max [104.0] average [29.79] 
[thread 13] INFO - L3: translateBarCode: min [9.0] max [129.0] average [37.94] 
[thread 5] INFO - L3: createOrder: min [269.0] max [1269.0] average [479.95]   
[thread 5] INFO - L3: translateBarCode: min [9.0] max [124.0] average [30.34]  
[thread 3] INFO - L3: createOrder: min [236.0] max [1238.0] average [492.35]   
[thread 3] INFO - L3: translateBarCode: min [10.0] max [108.0] average [32.04] 
[thread 16] INFO - L3: changeOrder: min [662.0] max [4204.0] average [1379.84] 
[thread 17] INFO - L3: createOrder: min [236.0] max [1335.0] average [521.18]  
[thread 16] INFO - L3: translateBarCode: min [10.0] max [112.0] average [34.87]
[thread 17] INFO - L3: translateBarCode: min [10.0] max [103.0] average [36.45]
[thread 13] INFO - L3: changeOrder: min [617.0] max [4094.0] average [1520.84] 
[thread 13] INFO - L3: translateBarCode: min [9.0] max [108.0] average [31.38] 
[thread 11] INFO - L3: changeOrder: min [620.0] max [4099.0] average [1316.38] 
[thread 5] INFO - L3: changeOrder: min [647.0] max [4154.0] average [1384.15]  
[thread 5] INFO - L3: translateBarCode: min [8.0] max [110.0] average [31.42]  
...
...

我能够使用基本的substr和Collections.sort类型的东西成功地将它变成这样的CVS格式:

API, Min, Max, Average
capturePayment, 232.0, 1800.0, 687.68
capturePayment, 268.0, 1853.0, 761.44
capturePayment, 301.0, 2612.0, 753.69
capturePayment, 309.0, 2632.0, 766.31
...
...

我的问题是,我希望将每个API的所有重复API时间平均化为一个条目(即每个API的平均最小/最大/平均值)。原始文件中有重复项,并且没有排序,因此我不确定如何继续。

我最大的问题是API的数量并不总是相同,即可能有10个capturePayment调用,但有20个createOrders。否则我有一个粗略的工作模型。有人可以给我一些指示吗?

[编辑]

使用下面的“registerAPI”解决方案,我几乎就在那里。计算的平均值与Excel中的平均值略有差异。这是我的代码。我唯一的想法可能就是从弦乐演奏到双打,这会让人精神错乱吗?

    while ((line = br.readLine()) != null) {
        if (line.contains("L3")) {
            int x,y;
            x = line.indexOf("L3: ") + "L3: ".length();
            y = line.indexOf(":", x);
            String name = line.substring(x,y).trim();
            x = line.indexOf("min [") + "min [".length();
            y = line.indexOf("]", x);
            String min = line.substring(x,y).trim();
            x = line.indexOf("max [") + "max [".length();
            y = line.indexOf("]", x);
            String max = line.substring(x,y).trim();
            x = line.indexOf("average [") + "average [".length();
            y = line.indexOf("]", x);
            String average = line.substring(x,y).trim();
            pStreamArray.add(name + ", " + min + ", " + max + ", " + average);
            double[] apiValues = new double[3];
            apiValues[0] = Double.valueOf(min);
            apiValues[1] = Double.valueOf(max);
            apiValues[2] = Double.valueOf(average);
            parseAPILogs.registerAPI(name, apiValues);
        }
    }

    Iterator iterator = averagePerAPI.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next().toString();
        double[] values = averagePerAPI.get(key);
        String valueString = "";
        for (int i = 0; i < values.length; i++) {
            valueString += values[i] + ", ";
        }
        System.out.println(key + " " + valueString); 
        pStreamCombined.println(key + " " + valueString);
    }

[编辑]

我在上面的代码中发现了数学上的缺陷 - 3个数字的平均值不等于前两个数字的平均值,然后是第三个数字的平均值。

实施例: (395 + 415 + 412)/3=407.33

(395 + 415)/ 2 = 405 (405 + 412)/2=408.5

1 个答案:

答案 0 :(得分:1)

我认为,您可以使用Map和平均容器来实现此目的。

容器是一个类,其当前总和为MINMAXAVERAGE以及当前计数:

public class APIData {

    private double min;
    private double max;
    private double average;
    private int amount;

    public void addValues(double[] values) {
        min += values[0];
        max += values[1];
        average += values[2];
        amount++;
    }

    public double getAPIMin() {
        return min / amount;
    }

    public double getAPIMax() {
        return max / amount;
    }

    public double getAPIAverage() {
        return average / amount;
    }

}

点击地图:

Map<String,APIData> averagePerAPI = new LinkedHashMap<String,int[]>();

此地图的键是API的名称,值为double的数组,大小为3,由于上述代码的逻辑,您可以按特定顺序保存{MIN, MAX, AVERAGE}

最后,您只需使用它提供的不同方法来管理地图。例如,使用方法registerAPI来保存API的数据更新现有数据。

public void registerAPI(String apiName, double[] apiValues) {
    if(!averagePerAPI.containsKey(apiName)) {
        APIData data = new APIData();
        data.addValues(apiValues)
        averagePerAPI.put(apiName, data);
    } else {
        averagePerAPI.get(apiName).addValues(apiValues);
    }
}

如果您想知道API的特定值,只需调用getAPIMin()getAPIMax()getAPIAverage()方法。