package com.hzs.common.core.utils;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/3/10 15:56
 * @Classname: distinctByKeyUtil
 * @PackageName: com.hzs.common.core.utils
 */

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 *@BelongsProject: hzs_cloud
 *@BelongsPackage: com.hzs.common.core.utils
 *@Author: yh
 *@CreateTime: 2023-03-10  15:56
 *@Description: TODO
 *@Version: 1.0
 */
public class DistinctByKeyUtil {
    /**
     * 对象根据某一值去重
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> set = ConcurrentHashMap.newKeySet();
        return t -> set.add(keyExtractor.apply(t));
    }
}
