Java 8 Stream of LocalDateTime,如何在不循环所有内容的情况下找到

我有一个 Java 8 LocalDateTime(时间戳)列表/流,它按升序(时间顺序)排列。


2019-03-01T13:13:13

2019-03-01T15:15:15

2019-03-02T12:12:12

2019-03-02T14:14:14

2019-03-03T11:11:11

2019-03-03T08:08:08

如何在不循环 list 的所有元素的情况下快速找出给定 12 小时内的子列表。我知道只做一个过滤器会很简单,但它会循环整个列表(假设列表很大)


如果开始时间戳是2019-03-01T10:10:10,那么结束时间戳是2019-03-01T22:22:21


时间戳的子列表必须在开始之后和结束之前。


慕慕森
浏览 108回答 3
3回答

幕布斯7119047

您可以尝试查看列表的两端,对索引进行计数,直到找到在所需开始时间之后或等于所需开始时间的第一个索引,同时向后迭代以获取您要查找的时间段中最后一次的索引。就像是:int first;for (LocalDateTime time : timeStampList){&nbsp; &nbsp; if (time.compareTo(startTime) < 0){&nbsp; &nbsp; &nbsp; &nbsp; first++;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; return i + 1;&nbsp; &nbsp; }}int last = list.length();while (last > 0){&nbsp; &nbsp; LocalDateTime time = timeStampList[last];&nbsp; &nbsp; if (time.compareTo(endTime) > 0){&nbsp; &nbsp; &nbsp; &nbsp; last -= 1;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp;return last - 1;&nbsp; &nbsp; }}然后,您要查找的子集将是这些索引之间的所有内容(包括)

临摹微笑

您可以使用subSet方法NavigableSetNavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)它可能是这样的:NavigableSet<LocalDateTime> treeSet = new TreeSet<>(yourListWithTimestamps);//Initialize your start and end date-times:LocalDateTime start = LocalDateTime.parse("2019-03-01T10:10:10");LocalDateTime end = LocalDateTime.parse("2019-03-01T22:22:21");NavigableSet<LocalDateTime> subSet = treeSet.subSet(start, false, end, false);//Optional - convert it back to list:List<LocalDateTime> subList = new ArrayList<>(subSet);

红糖糍粑

我可以向您推荐以下完全不使用 drop 的代码:import java.time.Duration;import java.time.LocalDateTime;import java.util.Arrays;import java.util.List;import java.util.function.BiPredicate;import java.util.stream.Collectors;import java.util.stream.IntStream;public class SublistWithLambda {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<LocalDateTime> dates = Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime.now().minusHours(24),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime.now().minusHours(22),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime.now().minusHours(20),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime.now().minusHours(12),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime.now().minusHours(10),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime.now().minusHours(7),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime.now().minusHours(5)&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; BiPredicate<LocalDateTime, LocalDateTime> isLessThan12Hours = (date1, date2) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Duration duration = Duration.between(date2, date1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return duration.toHours() >= 0 && duration.toHours() <= 12;&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; List<List<LocalDateTime>> result = IntStream&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .range(0, dates.size())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(i -> dates.stream().skip(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .takeWhile(date -> isLessThan12Hours.test(date, dates.get(i)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp; result.forEach(System.out::println);&nbsp; &nbsp; }}我希望这就是你要找的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java