我有一个List<Timeslot>
,其中包含实体Timeslot
和以下字段:
-
timeslot_id
; -
day
; -
start_time
; -
end_time
.
例如,此list包含两条记录:
-
第一条记录的
start_time
等于9:00
,end_time
等于10:00
. -
第二个物体的
start_time
等于10:00
,end_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_time
和end_time
之间的差值为one hour.
因此,根据上面提供的timestamps个列表构建的result应该包含六个对象:
-
start_time
是8:00
,end_time
是9:00
. -
start_time
是11:00
,end_time
是12:00
. -
start_time
是12:00
,end_time
是13:00
...等等
I第三个列表中不会出现带有start_time
9:00
和10:00
的对象,因为它们已被预订(present in the first list).
我try 使用Java Streams创建third list,它应该将字段start_time
和end_time
与second 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;
}