首页 > 学院 > 开发设计 > 正文

java8 stream distinct by key

2019-11-14 10:52:48
字体:
来源:转载
供稿:网友
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());});
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表