简体   繁体   English

关于简化循环迭代相关代码的问题

[英]Question about simplifying code related to loop iteration

I have a question on coding.我有一个关于编码的问题。 I get a task which is to iterate a list of elements to do dedupe.我得到一个任务,它是迭代一个元素列表来执行重复数据删除。

The list of elements are ordered by time and are divided into sub time windows.元素列表按时间排序并划分为子时间窗口。 In each sub time window, I need to pick up the deduplicated element per some business logic.在每个子时间窗口中,我需要根据某些业务逻辑选取已删除重复的元素。 Per the business logic, the deduplicated element in a sub time window is only known when all elements in the sub time window are iterated.根据业务逻辑,子时间窗口中的去重元素只有在子时间窗口中的所有元素都被迭代时才知道。 My question is how to simplify coding for this task.我的问题是如何简化此任务的编码。

The best I can think of is some pseudo-code like below我能想到的最好的是一些伪代码,如下所示

List<Element> deduplicated = new List();
Element subWindowPeekSignal = null;
Time subWindowEndTime = null;

elements.foreach(e -> {
      if (subWindowPeekElement != null
           && subWindowEndTime != null
           && isElementInTimeWindow(e, subWindowEndTime) {
         if (isIteratedElementHigherPriority(subWindowPeekElement, e)) {
             subWindowPeekElement = e;
         }
      } else {
         if (subWindowPeekElement != null) {
            deduplicated.add(subWindowPeekElement);
         }
         subWindowPeekElement = e;
         subWindowEndTime = getTime(e);
      }
  });

  //the ugly statement outside of loop
  deduplicated.add(subWindowPeekElement);

Specifically, how to avoid the last statement outside of the loop?具体来说,如何避免循环外的最后一条语句? Is it possible to finish the task of collecting all deduplicated elements within a loop or a use of java stream?是否有可能完成在循环或使用 java 流中收集所有重复数据删除元素的任务?

Many thanks, Jun非常感谢,君

根据我团队的讨论,这是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM