I have List<Timeslot>
that contains entity Timeslot with the following fields:
- timeslot_id
- day
- start_time
- end_time
In my case, this List contains two records. The first record has start_time
set to 9:00 and end_time
set to 10:00. The second record instead has start_time
set to 10:00 while end_time
set to 11:00.
Besides the previous list, there's also a second one which contains the following timestamps 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]
I'd like to obtain a third list List<Timeslot>
containing Timeslot elements initialized with the timestamps from the second list. Each timestamp sets a Timeslot's start_time', while the
end_time` is simply set one hour forward its start_time. Furthermore, the final list must not contain the two elements from the first one.
I tried to create the third list by using Java Streams, all I get is an empty list:
List<Timeslot> availableSlots = query.stream()
.filter(timeslot -> timestamps.contains(timeslot.getStartTime()))
.toList();
Timeslot class:
@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;
}
CodePudding user response:
I've simplified your Timeslot
class for this problem (for demonstration purposes) since for this task you primarily concern about the start time and end time of each timeslot.
My approach is to create a set of LocalDateTime
objects by extracting the start time from each timeslot that is already taken (represented by your first list).
Then create a stream over the query
list and filter the date-time object that are not present in the set. Then create a timeslot using each date-time object as a start time (end time = start time 1 hour). And collect all the stream elements into a list.
Note: terminal operation toList()
creates an immutable list, you can obtain a mutable list by applying collect(Collectors.toList())
instead.
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);
}
Simplified dummy Timeslot
class
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}