yangys
2024-10-30 25db770e621f1259b8d5b7fd514207f7481c2d0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.qianwen.smartman.common.utils;
 
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
 
public final class DistinctKeyUtil {
    private DistinctKeyUtil() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }
 
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>(16);
        return t -> {
            return map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
        };
    }
}