使用Java 8将LocalDateTime对象分组到间隔中

huangapple 未分类评论71阅读模式
英文:

Grouping LocalDateTime objects in intervals using Java 8

问题

我有一个以以下格式排列的列表,我想将此列表分成一分钟间隔。

  1. List<Item> myObjList = Arrays.asList(
  2. new Item(LocalDateTime.parse("2020-09-22T00:13:36")),
  3. new Item(LocalDateTime.parse("2020-09-22T00:17:20")),
  4. new Item(LocalDateTime.parse("2020-09-22T01:25:20")),
  5. new Item(LocalDateTime.parse("2020-09-18T00:17:20")),
  6. new Item(LocalDateTime.parse("2020-09-19T00:17:20")));

例如,给定一个间隔为10分钟,列表的前两个对象应在同一组中,第3个应在另一组中,依此类推。

这个列表可以使用Java 8的groupingBy函数分成间隔吗?

我的解决方案是将列表中的每个日期与列表中的所有其他日期进行比较,并将相差X分钟的日期添加到新列表中。这似乎非常慢且不太正规,我想知道是否有更稳定的解决方案。

英文:

I have a List in the following format and I want to group this List into minute intervals.

  1. List&lt;Item&gt; myObjList = Arrays.asList(
  2. new Item(LocalDateTime.parse(&quot;2020-09-22T00:13:36&quot;)),
  3. new Item(LocalDateTime.parse(&quot;2020-09-22T00:17:20&quot;)),
  4. new Item(LocalDateTime.parse(&quot;2020-09-22T01:25:20&quot;)),
  5. new Item(LocalDateTime.parse(&quot;2020-09-18T00:17:20&quot;)),
  6. new Item(LocalDateTime.parse(&quot;2020-09-19T00:17:20&quot;)));

For example, given an interval of 10 minutes the first 2 objects of the list should be in the same group, the 3rd should be in a different group, etc.

Can this List be grouped into intervals using Java's 8 groupingBy function?

My solution is to compare every date in the list with all the other dates in the list and add the dates that differ X minutes in a new List. This seems to be very slow and 'hacky' workaround and I wonder if there is a more stable solution.

答案1

得分: 6

这里可以使用 Collectors#groupingBy 方法将 LocalDateTime 对象分组为以10分钟为间隔的列表。您需要根据您的 Item 类来调整这个代码片段,但逻辑是相同的。

  1. List<LocalDateTime> myObjList = Arrays.asList(
  2. LocalDateTime.parse("2020-09-22T00:13:36"),
  3. LocalDateTime.parse("2020-09-22T00:17:20"),
  4. LocalDateTime.parse("2020-09-22T01:25:20"),
  5. LocalDateTime.parse("2020-09-18T00:17:20"),
  6. LocalDateTime.parse("2020-09-19T00:17:20")
  7. );
  8. System.out.println(myObjList.stream().collect(Collectors.groupingBy(time -> {
  9. // 存储小时内的分钟字段。
  10. int minutes = time.getMinute();
  11. // 确定我们超过最近的10分钟间隔多少分钟。
  12. int minutesOver = minutes % 10;
  13. // 截断时间到分钟字段(将秒和纳秒清零),
  14. // 并将分钟数强制为10分钟的间隔。
  15. return time.truncatedTo(ChronoUnit.MINUTES).withMinute(minutes - minutesOver);
  16. })));

输出结果:

  1. {
  2. 2020-09-22T00:10=[2020-09-22T00:13:36, 2020-09-22T00:17:20],
  3. 2020-09-19T00:10=[2020-09-19T00:17:20],
  4. 2020-09-18T00:10=[2020-09-18T00:17:20],
  5. 2020-09-22T01:20=[2020-09-22T01:25:20]
  6. }
英文:

It is possible to use Collectors#groupingBy to group LocalDateTime objects into lists of 10-minute intervals. You'll have to adapt this snippet to work with your Item class, but the logic is the same.

  1. List&lt;LocalDateTime&gt; myObjList = Arrays.asList(
  2. LocalDateTime.parse(&quot;2020-09-22T00:13:36&quot;),
  3. LocalDateTime.parse(&quot;2020-09-22T00:17:20&quot;),
  4. LocalDateTime.parse(&quot;2020-09-22T01:25:20&quot;),
  5. LocalDateTime.parse(&quot;2020-09-18T00:17:20&quot;),
  6. LocalDateTime.parse(&quot;2020-09-19T00:17:20&quot;)
  7. );
  8. System.out.println(myObjList.stream().collect(Collectors.groupingBy(time -&gt; {
  9. // Store the minute-of-hour field.
  10. int minutes = time.getMinute();
  11. // Determine how many minutes we are above the nearest 10-minute interval.
  12. int minutesOver = minutes % 10;
  13. // Truncate the time to the minute field (zeroing out seconds and nanoseconds),
  14. // and force the number of minutes to be at a 10-minute interval.
  15. return time.truncatedTo(ChronoUnit.MINUTES).withMinute(minutes - minutesOver);
  16. })));

Output

  1. {
  2. 2020-09-22T00:10=[2020-09-22T00:13:36, 2020-09-22T00:17:20],
  3. 2020-09-19T00:10=[2020-09-19T00:17:20],
  4. 2020-09-18T00:10=[2020-09-18T00:17:20],
  5. 2020-09-22T01:20=[2020-09-22T01:25:20]
  6. }

答案2

得分: 2

你没有指定分组的关键字,所以我只是使用了(minutes/10)*10的商来获取分钟范围的起始,并将其附加到截断到小时的时间上。

以下是您提供的代码部分的翻译:

  1. List<Item> myObjList = Arrays.asList(
  2. new Item(LocalDateTime.parse("2020-09-22T00:13:36")),
  3. new Item(LocalDateTime.parse("2020-09-22T00:17:20")),
  4. new Item(LocalDateTime.parse("2020-09-22T01:25:20")),
  5. new Item(LocalDateTime.parse("2020-09-18T00:17:20")),
  6. new Item(LocalDateTime.parse("2020-09-19T00:17:20")));
  7. Map<String, List<Item>> map = myObjList.stream()
  8. .collect(Collectors.groupingBy(item -> {
  9. int range = (item.getTime().getMinute() / 10) * 10;
  10. return item.getTime().truncatedTo(ChronoUnit.HOURS).plusMinutes(range) +
  11. " - " + (range + 9) + ":59";
  12. }));
  13. map.entrySet().forEach(System.out::println);

输出结果为:

  1. 2020-09-22T00:10 - 19:59=[2020-09-22T00:13:36, 2020-09-22T00:17:20]
  2. 2020-09-19T00:10 - 19:59=[2020-09-19T00:17:20]
  3. 2020-09-18T00:10 - 19:59=[2020-09-18T00:17:20]
  4. 2020-09-22T01:20 - 29:59=[2020-09-22T01:25:20]

以下是您使用的Item类:

  1. class Item {
  2. LocalDateTime ldt;
  3. public Item(LocalDateTime ldt) {
  4. this.ldt = ldt;
  5. }
  6. public String toString() {
  7. return ldt.toString();
  8. }
  9. public LocalDateTime getTime() {
  10. return ldt;
  11. }
  12. }
英文:

You didn't specify the key for the groups so I just used the quotient of (minutes/10)*10 to get the start of the range of minutes tagged onto the time truncated to hours.

  1. List&lt;Item&gt; myObjList = Arrays.asList(
  2. new Item(LocalDateTime.parse(&quot;2020-09-22T00:13:36&quot;)),
  3. new Item(LocalDateTime.parse(&quot;2020-09-22T00:17:20&quot;)),
  4. new Item(LocalDateTime.parse(&quot;2020-09-22T01:25:20&quot;)),
  5. new Item(LocalDateTime.parse(&quot;2020-09-18T00:17:20&quot;)),
  6. new Item(LocalDateTime.parse(&quot;2020-09-19T00:17:20&quot;)));
  7. Map&lt;String, List&lt;Item&gt;&gt; map = myObjList.stream()
  8. .collect(Collectors.groupingBy(item -&gt; {
  9. int range =
  10. (item.getTime().getMinute() / 10) * 10;
  11. return item.getTime()
  12. .truncatedTo(ChronoUnit.HOURS).plusMinutes(range) +
  13. &quot; - &quot; + (range + 9) + &quot;:59&quot;;
  14. }));
  15. map.entrySet().forEach(System.out::println);

Prints

  1. 2020-09-22T00:10 - 19:59=[2020-09-22T00:13:36, 2020-09-22T00:17:20]
  2. 2020-09-19T00:10 - 19:59=[2020-09-19T00:17:20]
  3. 2020-09-18T00:10 - 19:59=[2020-09-18T00:17:20]
  4. 2020-09-22T01:20 - 29:59=[2020-09-22T01:25:20]

Here is the class I used.

  1. class Item {
  2. LocalDateTime ldt;
  3. public Item(LocalDateTime ldt) {
  4. this.ldt = ldt;
  5. }
  6. public String toString() {
  7. return ldt.toString();
  8. }
  9. public LocalDateTime getTime() {
  10. return ldt;
  11. }
  12. }
  13. </details>

huangapple
  • 本文由 发表于 2020年8月20日 23:03:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/63507920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定