bnrmaster
7/1/2019 - 4:13 PM

distinctByKey.java

    /**
     去重辅助函数
     @param keyExtractor
     @param <T>
     @return
     */
    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
    
    
     // 去重
    List<Mistake> mistakes = mistakes.stream().filter(distinctByKey(Mistake::getQuestionUrl)).collect(Collectors.toList());
    mistakes.forEach(mistake ->
            mistake.setQuestionUrl(urlMap.get(Long.parseLong(mistake.getQuestionUrl())))
    );