본문 바로가기
실습기록

7월 14일 project - 중복데이터 잡기, DeduplicationUtils

by project100 2023. 7. 16.
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));
    }
}