【问题标题】:Find duplicates in first column and take average based on third column在第一列中查找重复项并根据第三列取平均值
【发布时间】: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


【解决方案1】:

我不知道您完成平均时间的逻辑是什么,但您可以将数据保存在地图中以用于一项特定交易。地图结构可以是这样的。交易 id 将是关键,并且一直在数组列表中。

Map<String,List<String>> map = new HashMap<String,List<String>>();

【讨论】:

    【解决方案2】:

    你可以这样做:

     Map<String, String> result = Files.lines(Paths.get("abc.txt"))
                .map(line -> line.split(","))
                .map(arr -> {
                    try {
                        return new AbstractMap.SimpleEntry<>(arr[0],
                                              new SimpleDateFormat("HH:mm").parse(arr[2]));
                    } catch (ParseException e) {
                        return null;
                    }
                }).collect(Collectors.groupingBy(Map.Entry::getKey,
                        Collectors.collectingAndThen(Collectors
                                        .mapping(Map.Entry::getValue, Collectors.toList()),
                                list -> toStringTime.apply(convert.apply(list)))));
    

    为了简化,我声明了两个函数。

    Function<List<Date>, Long> convert = list -> (list.get(1).getTime() - list.get(0).getTime()) / 2;
    Function<Long, String> toStringTime = l -> l / 60000 + ":" + l % 60000 / 1000;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      相关资源
      最近更新 更多