julkor
julkor

Reputation: 3

How to filter stream in java with few parameters?

I need to filter list of visits by few parameters. I know that this is not good solution. When there will be a lot of visits I can't get all of them to just get for example few of them. I was thinking about Querydsl? Anyone have good solution? this is my method:

private List<Visit> findVisitByParameters(CustomSearchCommand visit){
List<Visit> allVisits = repository.findAll();
List<Visit> visitList = allVisits.stream()
        .filter(v -> v.getDate().isAfter(visit.getDateFrom()))
        .filter(v -> v.getDate().isBefore(visit.getDateTo()))
        .filter(v -> v.getVeterinarian().getAnimalType().equals(visit.getAnimalType()))
        .filter(v -> v.getVeterinarian().getMedicalSpecialization().equals(visit.getMedicalSpecialization()))
        .collect(Collectors.toList());
if(visitList.isEmpty()) throw new InvalidParameterException("There are not any visits in this time period with" +
        " this animal type and medical Specialization");
return visitList;

}

Upvotes: 0

Views: 160

Answers (1)

pringi
pringi

Reputation: 4652

Yes, you can use QueryDsl, Jooq, Spring Data, hibernate, etc to do that.

Example of using Spring Data:

@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
User findUserByStatusAndName(Integer status, String name);

Example taken from: https://www.baeldung.com/spring-data-jpa-query

Upvotes: 1

Related Questions