본문 바로가기
실습기록

7월 6일 project - 검색 필터 기능, 중복 제거

by project100 2023. 7. 8.
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class DeduplicationUtils {
    public static <T> List<T> deduplication(final List<T> list, Function<? super T,?> key){
        return list.stream().filter(deduplication(key))
                .collect(Collectors.toList());
    }
   private static <T> Predicate<T> deduplication(Function<? super T,?> key){
        final Set<Object> set = ConcurrentHashMap.newKeySet();
        return predicate -> set.add(key.apply(predicate));
    }
}

 

public ModelAndView hGymFilterList(String ggoodsname, String mgender, List<String> check) {
        log.info("hGymFilterList()");
        mv = new ModelAndView();
        Map<String, String> fMap = new HashMap<>();
        fMap.put("ggoodsname", ggoodsname);
        fMap.put("mgender", mgender);

        String andQuery = "";
        //List<String> check =
        for(String i : check){
            switch (i){
                case "1":
                    andQuery += " AND gc_cloths = 1";
                    break;
                case "2":
                    andQuery = " AND gc_cloths = 1";
                    break;
                case "3":
                    andQuery += " AND gc_towel = 1";
                    break;
                case "4":
                    andQuery += " AND gc_wifi = 1";
                    break;
                case "5":
                    andQuery += " AND gc_parking = 1";
                    break;
                case "6":
                    andQuery += " AND gc_inbody = 1";
                    break;
            }
        }

        fMap.put("andQuery", andQuery);

        List<FillterViewDto> aList = gDao.hGymFilteraList(fMap);
        List<FillterViewDto> fList = DeduplicationUtils.deduplication(aList, FillterViewDto::getGname);

        mv.addObject("fList", fList);
        mv.setViewName("gymSetList");
        return mv;
    }