编写工具类
public class DistinctUtil { public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Map<Object, Boolean> seen = new ConcurrentHashMap<>(); return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; } }
使用方式如下(意思是:根据releaseVersions是一个List<MDynamicFuncReleaseVersion>,然后根据MDynamicFuncReleaseVersion的ReleaseVersion属性对List进行数据去重):
releaseVersions.stream().filter(DistinctUtil.distinctByKey(MDynamicFuncReleaseVersion::getReleaseVersion)).collect(Collectors.toList());