我有一个List<Timeslot>,其中包含实体Timeslot和以下字段:

  1. timeslot_id;
  2. day;
  3. start_time;
  4. end_time.

例如,此list包含两条记录:

  • 第一条记录的start_time等于9:00end_time等于10:00.

  • 第二个物体的start_time等于10:00end_time等于11:00.

第二个list包含时间戳List<LocalDateTime>:

[2022-04-16T08:00, 2022-04-16T09:00, 2022-04-16T10:00, 2022-04-16T11:00, 
 2022-04-16T12:00, 2022-04-16T13:00, 2022-04-16T14:00, 2022-04-16T15:00]

我需要创建第三个List<Timeslot>,其中将包含Timeslot个,除了第一个列表中的这两个.

因此,在本例中,third list应该包含6个Timeslot类的对象.

第一个的start_time应该等于2022-04-16T08:00,第二个等于2022-04-16T09:00.也就是说,start_timeend_time之间的差值为one hour.

因此,根据上面提供的timestamps个列表构建的result应该包含六个对象:

  • start_time8:00end_time9:00.
  • start_time11:00end_time12:00.
  • start_time12:00end_time13:00...等等

I第三个列表中不会出现带有start_time 9:0010:00的对象,因为它们已被预订(present in the first list).

我try 使用Java Streams创建third list,它应该将字段start_timeend_timesecond list的时间戳进行比较.

我try 过这个,但结果列表总是空的:

List<Timeslot> availableSlots = query.stream()
    .filter(timeslot -> timestamps.contains(timeslot.getStartTime()))
    .toList();

Timeslot级:

@Entity(name = "timeslot")
public class Timeslot {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "timeslot_id")
    private Integer id;
    @Column(name = "day", columnDefinition = "DATE")
    private LocalDateTime day;
    @Column(name = "start_time")
    private LocalDateTime startTime;
    @Column(name = "end_time")
    private LocalDateTime endTime;
    @Column(name = "user_id")
    private Integer userId;
    @Column(name = "is_recorded")
    private Boolean isRecorded;
}

推荐答案

对于这个问题,我简化了你的Timeslot课(for demonstration purposes),因为对于这个任务,你主要关注timeslot中的start timeend time.

我的方法是通过从已经获取的timeslot个对象中提取start time个来创建一组LocalDateTime个对象(由第一个列表表示).

然后在query列表上创建一个流,并过滤集合中不存在的日期时间对象.然后使用每个日期时间对象创建一个timeslot作为start time(end time=start time+1小时).并将所有流元素收集到一个list中.

Note:终端操作toList()创建一个不可变列表,您可以通过应用collect(Collectors.toList())获得一个可变列表.

public static void main(String[] args) {
    List<LocalDateTime> query =
        List.of(LocalDateTime.of(2022, 04, 16, 8, 00),
                LocalDateTime.of(2022, 04, 16, 9, 00),
                LocalDateTime.of(2022, 04, 16, 10, 00),
                LocalDateTime.of(2022, 04, 16, 11, 00),
                LocalDateTime.of(2022, 04, 16, 12, 00),
                LocalDateTime.of(2022, 04, 16, 13, 00),
                LocalDateTime.of(2022, 04, 16, 14, 00),
                LocalDateTime.of(2022, 04, 16, 15, 00));

    List<Timeslot> timeslots = // timeslots that already taken
        List.of(new Timeslot(LocalDateTime.of(2022, 04, 16, 9, 00),
                             LocalDateTime.of(2022, 04, 16, 10, 00)),
                new Timeslot(LocalDateTime.of(2022, 04, 16, 10, 00),
                             LocalDateTime.of(2022, 04, 16, 11, 00)));
    
    Set<LocalDateTime> takenStartTime = timeslots.stream()
        .map(Timeslot::getStartTime)
        .collect(Collectors.toSet());

    List<Timeslot> availableSlots = query.stream()
        .filter(dateTime -> !takenStartTime.contains(dateTime))
        .map(dateTime -> new Timeslot(dateTime, dateTime.plusHours(1)))
        .toList();
    
    availableSlots.forEach(System.out::println);
}

简化假人Timeslot

public class Timeslot {
    private LocalDateTime startTime;
    private LocalDateTime endTime;
    
    // constructor, getters, toString()
}

Output

Timeslot{start_time=2022-04-16T08:00, end_time=2022-04-16T09:00}
Timeslot{start_time=2022-04-16T11:00, end_time=2022-04-16T12:00}
Timeslot{start_time=2022-04-16T12:00, end_time=2022-04-16T13:00}
Timeslot{start_time=2022-04-16T13:00, end_time=2022-04-16T14:00}
Timeslot{start_time=2022-04-16T14:00, end_time=2022-04-16T15:00}
Timeslot{start_time=2022-04-16T15:00, end_time=2022-04-16T16:00}

Java相关问答推荐

将具有多个未知字段的SON映射到Java POJO

Java中后期绑定的替代概念

我需要生成一个文件来整合每个特性执行的所有JSON结果

内存中的H2修剪尾随空格

如何在ApachePOI中将图像添加到工作表的页眉?

计算两个浮点数之间的距离是否对称?

Kotlin内联互操作:强制装箱

Jolt变换JSON数组问题

声明MessageChannel Bean的首选方式

如何从日志(log)行中删除包名称?

继续收到错误SQLJDBC EXCEPTION执行";org.springframework.dao.InvalidDataAccessResourceUsageException:&

垃圾收集时间长,会丢弃网络连接,但不会在Kubernetes中反弹Pod

根本不显示JavaFX阿拉伯字母

try 使用类来包含JSON响应

如何使用MapStrCut转换双向链接

在Java中将对象&转换为&q;HashMap(&Q)

具有多个分析模式的复杂分隔字符串的正则表达式

使用原子整数的共享计数器并发增量

[Guice/MissingImplementation]:未绑定任何实现

ANTLR 接受特殊字符,例如 .标识符或表达式中的(点)和 ,(逗号)