I have a controller with some @RequestParam(required = false) which have to return all medicines with some filters if they are exist, service for this controller, which contains logic for it, but it is not working. The main problem is that I have 3 ArrayList and I cannot come up with how to find all elements which exist in all three ArrayLists :)
    public List<Medicine> readAllMedicine(Double lessThenPrice, Double moreThenPrice, String name) {
    //when lessThenPrice < moreThenPrice, result of their queries will not have general elements
    if ((lessThenPrice != null && moreThenPrice != 0) && lessThenPrice < moreThenPrice) {
        return Collections.emptyList();
    }
    List<Medicine> resultList = new ArrayList<>();
    List<Medicine> lessList = new ArrayList<>();
    List<Medicine> moreList = new ArrayList<>();
    List<Medicine> nameList = new ArrayList<>();
    //checking if there are arguments from the controller
    if (lessThenPrice != null) {
        lessList.addAll(medicineDao.findMedicinesByPriceIsLessThan(lessThenPrice));
    }
    if (moreThenPrice != null) {
        moreList.addAll(medicineDao.findMedicinesByPriceIsGreaterThan(moreThenPrice));
    }
    if (name != null) {
        nameList.addAll(medicineDao.findMedicinesByName(name));
    }
    //saving general elements
    //this part is not working
    if (!lessList.isEmpty() || !moreList.isEmpty() || !nameList.isEmpty()) {
        List<Medicine> temp = new ArrayList<>(); //it will contain general part of lessList and moreList
        for (Medicine medicine : lessList) {
            if (moreList.contains(medicine)) {
                temp.add(medicine);
            }
        }
        for (Medicine medicine : nameList) {
            if (temp.contains(medicine)) {
                resultList.add(medicine);
            }
        }
        return resultList;
    }
    //if there is no args, just return all medicines
    return medicineDao.findAll();
}
 
     
     
    