红糖糍粑
我可以向您推荐以下完全不使用 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 { public static void main(String[] args) { List<LocalDateTime> dates = Arrays.asList( LocalDateTime.now().minusHours(24), LocalDateTime.now().minusHours(22), LocalDateTime.now().minusHours(20), LocalDateTime.now().minusHours(12), LocalDateTime.now().minusHours(10), LocalDateTime.now().minusHours(7), LocalDateTime.now().minusHours(5) ); BiPredicate<LocalDateTime, LocalDateTime> isLessThan12Hours = (date1, date2) -> { Duration duration = Duration.between(date2, date1); return duration.toHours() >= 0 && duration.toHours() <= 12; }; List<List<LocalDateTime>> result = IntStream .range(0, dates.size()) .mapToObj(i -> dates.stream().skip(i) .takeWhile(date -> isLessThan12Hours.test(date, dates.get(i))) .collect(Collectors.toList())) .collect(Collectors.toList()); result.forEach(System.out::println); }}我希望这就是你要找的。