If this is a Spring-based project you can use vavr's collections instead of java's. Thats is, MessageRepository.findAll
can return a list of type io.vavr.collection.List
. So it may look as follows:
import io.vavr.collection.List;
import io.vavr.control.Try;
import java.time.Instant;
class Main {
public static void main(String[] args) {
var repo = new MessageRepository();
var summarized = repo
.findAll()
.mapTry(
messages -> messages.map(message -> new SummarizedMessage(message.title(), message.destination(), message.date()))
)
.getOrElse(List.empty());
System.out.println(summarized);
}
}
record Message(String title, String origin, String destination, String text, Instant date) {
}
record SummarizedMessage(String title, String destination, Instant date) {
}
class MessageRepository {
Try<List<Message>> findAll() {
return Try.of(() -> List.of(
new Message("title", "origin", "dest", "text", Instant.now())
));
}
}
It allows you to drop unnecessary .stream()
call. Also instead of null
it's better to return an empty list (List.empty
of List.of
) but it may depend on your logic.