当前位置:网站首页>stream去重相同属性对象
stream去重相同属性对象
2022-08-09 14:53:00 【Java升级之路】
近日遇到一个list集合,里面有重复的对象,需要去重,因为遇到的比较少,特此记录一下处理方式:
@Test
public void streamTest8() {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("鸣人", 8900, 18, "1", "螺旋丸"));
personList.add(new Person("鸣人", 8900, 18, "1", "螺旋丸"));
personList.add(new Person("小樱", 7800, 17, "2", "治疗术"));
personList.add(new Person("小樱", 7800, 17, "2", "治疗术"));
personList.add(new Person("大蛇丸", 9500, 31, "1", "八岐大蛇"));
personList.add(new Person("佐助", 8900, 18, "2", "须佐能乎"));
personList.add(new Person("纲手", 7900, 29, "2", "百豪之术"));
// list 一个条件 去重
List<Person> uniqueByName = personList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new));
System.out.println("一个条件去重后的list:" + uniqueByName);
// list 多个条件 去重
List<Person> uniqueBySalaryAndAge = personList.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(p -> p.getSalary() + ";" + p.getAge()))), ArrayList::new));
System.out.println("多个条件去重后的list:" + uniqueBySalaryAndAge);
}运行结果:

边栏推荐
- Welcome to use CSDN - markdown editor
- .Net Core后台任务启停(BackgroundService)
- What do professional quantitative traders think about quantitative trading?
- 【C语言初阶】详解分支语句
- 对程序化交易系统接口有什么误区?
- How to make your quantitative trading system have probabilistic advantages and positive return expectations?
- 深刻地认识到,编译器会导致编译结果的不同
- 职业量化交易员对量化交易有什么看法?
- How do quantitative investors obtain real-time market data?
- ASP.Net Core实战——使用Swagger
猜你喜欢
随机推荐
What do professional quantitative traders think about quantitative trading?
The recycle bin has been showed no problem to empty the icon
物理学专业英语(词汇整理)--------07
LNK1123: Failed during transition to COFF: invalid or corrupt file
Qt control - QTextEdit usage record
The difference between show and exec in Qt dialog
C语言运算符优先级
正则化原理的简单分析(L1/L2正则化)
docker安装单机版redis、集群版redis
流程控制学习
Entity Framework Core知识小结
ASP.Net Core实战——使用Swagger
方法学习笔记
focal loss原理及简单代码实现
如何保证电脑硬盘格式化后数据不能被恢复?
Qt对话框中show和exec的区别
如何通过通达信量化交易接口达到长期的收益?
In the process of quantitative trading, retail investors can do this
Grad CAM model visualization
经典面试题 之 TCP 三次握手/ 四次挥手








