当前位置: 首页 > news >正文

Java 8 Distinct by property

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Q: In Java 8 how can I filter a collection using the Stream API by checking the distinctness of a property of each object?

For example I have a list of Person object and I want to remove people with the same name,

persons.stream().distinct();

Will use the default equality check for a Person object, so I need something like,

persons.stream().distinct(p -> p.getName());

Unfortunately the distinct() method has no such overload. Without modifying the equality check inside the Person class is it possible to do this succinctly?

 

A1: Consider distinct to be a stateful filter. Write function that returns a predicate that also maintains state about what it's seen previously, and returns whether the given element was seen for the first time:

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;
}

Then you can write:

persons.stream().filter(distinctByKey(p -> p.getName());

 

A2:  An alternative would be to place the persons in a map using the name as a key:

persons.collect(toMap(Person::getName, p -> p, (p, q) -> p)).values();

Note that the Person that is kept, in case of a duplicate name, will be the first encontered.

转载于:https://my.oschina.net/u/2935389/blog/1551313

相关文章:

  • [译] Swift 中关于并发的一切:第一部分 — 当前
  • js的属性事件
  • 网络请求框架 Retrofit 2 使用入门
  • cpu,io密集型计算概念
  • ubuntu 安装dlib 出现dlib.so: undefined symbol: png_set_longjmp_fn
  • 第三讲课后作业1
  • 购物车优化
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • spring mvc 配置文件加载
  • 用canvas实现一个colorpicker
  • JDK8新特性(2):Stream API常用操作
  • BZOJ 2457 [BeiJing2011] 双端队列
  • 如何用TensorFlow生成令人惊艳的分形图案
  • Hive SQL 练习(这个秒退是怎么回事啊?写了半天 东西都没了,瞬间整个人都凌乱了)...
  • SylixOS之TFTP使用
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • docker容器内的网络抓包
  • eclipse(luna)创建web工程
  • Git初体验
  • Go 语言编译器的 //go: 详解
  • leetcode388. Longest Absolute File Path
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • MySQL用户中的%到底包不包括localhost?
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • React Native移动开发实战-3-实现页面间的数据传递
  • 工作中总结前端开发流程--vue项目
  • 和 || 运算
  • 简单易用的leetcode开发测试工具(npm)
  • 数组大概知多少
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • nb
  • 阿里云ACE认证学习知识点梳理
  • 组复制官方翻译九、Group Replication Technical Details
  • ​低代码平台的核心价值与优势
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • ​一、什么是射频识别?二、射频识别系统组成及工作原理三、射频识别系统分类四、RFID与物联网​
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (14)Hive调优——合并小文件
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (C++17) std算法之执行策略 execution
  • (day6) 319. 灯泡开关
  • (第27天)Oracle 数据泵转换分区表
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (南京观海微电子)——I3C协议介绍
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • ./configure、make、make install 命令
  • .bashrc在哪里,alias妙用
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .net core webapi 大文件上传到wwwroot文件夹
  • .NET Core 将实体类转换为 SQL(ORM 映射)