I want to fetch certain records from my CRUD repository using a list of ids and currently my code looks like this:
public List<Document> getDocuments(List<String> idList) {
List<Document> allDocumentList = repository.findAll();
List<Document> resultList = new ArrayList<>();
for (String id : idList) {
resultList.add(allDocumentList.stream()
.filter(document -> document.getId().equals(id))
.findFirst().get());
}
return resultList;
Is there a better, more efficient way to do this? Basically I'm thinking about a situation where the repository has a very large number of records.