当前位置:网站首页>对象型的集合按某个属性的值进行去重
对象型的集合按某个属性的值进行去重
2022-07-26 09:05:00 【Hejjon】
直接上代码吧, 哈哈哈
public class RemoveDuplicateTest {
public static void main(String[] args) {
List<User> userList = new ArrayList<>();
userList.add(new User("zs", 19, 80.0));
userList.add(new User("zs", 18, 76.0));
userList.add(new User("lisi", 20, 70.0));
userList.add(new User("zs", 21, 90.0));
userList.add(new User("lisi", 22, 60.0));
userList.add(new User("wangwu", 18, 86.0));
int num = 0;
for (User user : userList) {
System.out.println(user);
num++;
}
System.out.println("去重前数量: " + num);
// 按name属性去重
// 常规写法
Set<String> nameSet = new HashSet<>();
List<User> resList = new ArrayList<>();
for (User user : userList) {
if (!nameSet.contains(user.getName())) {
resList.add(user);
}
nameSet.add(user.getName());
}
int num2 = 0;
for (User user : resList) {
System.out.println(user);
num2++;
}
System.out.println("去重后数量: " + num2);
// 使用jdk8新特性写法
List<User> resList2 = userList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new
));
for (User user : resList2) {
System.out.println(user);
}
}
}
重点是jdk8新特性的写法
// 使用jdk8新特性写法
List<User> resList2 = userList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new
));
边栏推荐
- pycharm 打开多个项目的两种小技巧
- day06 作业--技能题2
- Error: Cannot find module ‘umi‘ 问题处理
- Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
- codeforces dp合集
- 基于序的评价指标 (特别针对推荐系统和多标签学习)
- 多项式开根
- JDBC database connection pool (Druid Technology)
- ONTAP 9文件系统的限制
- 187. Repeated DNA sequence
猜你喜欢
Study notes of automatic control principle -- correction and synthesis of automatic control system
QtCreator报错:You need to set an executable in the custom run configuration.
JDBC数据库连接池(Druid技术)
Review notes of Microcomputer Principles -- zoufengxing
TCP solves the problem of short write
Uni app simple mall production
Elastic APM安装和使用
at、crontab
187. Repeated DNA sequence
Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
随机推荐
Summary of common activation functions for deep learning
Grain College of all learning source code
Regular expression: judge whether it conforms to USD format
数据库操作 技能6
Nuxt - Project packaging deployment and online to server process (SSR server rendering)
CF1481C Fence Painting
Web概述和B/S架构
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
“No input file specified “问题的处理
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
Node-v download and application, ES6 module import and export
The largest number of statistical absolute values --- assembly language
QtCreator报错:You need to set an executable in the custom run configuration.
Error: Cannot find module ‘umi‘ 问题处理
机器学习中的概率模型
Pytoch learning - from tensor to LR
209. Subarray with the smallest length
Day06 homework - skill question 7
谷粒学院的全部学习源码
Elastic APM安装和使用