java8 stream distinct by key

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;
}
deviceRecords.stream().filter(distinctByKey(deviceRecord -> deviceRecord.getDeviceMac())).forEach(deviceRecord -> {
    System.out.println(deviceRecord.getDeviceMac());
});

    原文作者:hackcoder
    原文地址: https://blog.csdn.net/hackcoder/article/details/54863777
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞