当前位置:网站首页>Object type collections are de duplicated according to the value of an attribute
Object type collections are de duplicated according to the value of an attribute
2022-07-26 09:11:00 【Hejjon】
Go straight to the code , Ha ha ha
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(" Quantity before weight removal : " + num);
// Press name Attribute de duplication
// Conventional writing
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(" Quantity after weight removal : " + num2);
// Use jdk8 New feature writing
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);
}
}
}
The key is jdk8 How to write new features
// Use jdk8 New feature writing
List<User> resList2 = userList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new
));
边栏推荐
- Rocky基础练习题-shell脚本2
- Review notes of Microcomputer Principles -- zoufengxing
- Day06 homework - skill question 7
- PAT 甲级 A1076 Forwards on Weibo
- The child and binary tree- open root inversion of polynomials
- redis原理和使用-安装和分布式配置
- 《Datawhale熊猫书》出版了!
- 论文笔记: 知识图谱 KGAT (未完暂存)
- Pat grade a a1013 battle over cities
- Error: cannot find module 'UMI' problem handling
猜你喜欢
随机推荐
原根与NTT 五千字详解
Day 6 summary & database operation
(2006,Mysql Server has gone away)问题处理
Pop up window in Win 11 opens with a new tab ---firefox
Elastic APM安装和使用
Error: cannot find module 'UMI' problem handling
Pat grade a A1034 head of a gang
CF1481C Fence Painting
Learning notes of automatic control principle - Performance Analysis of continuous time system
Web overview and b/s architecture
unity简易消息机制
力扣刷题,三数之和
JS file import of node
Study notes of dataX
Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
第6天总结&数据库作业
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
Overview of motion recognition evaluation
220. Presence of repeating element III
Datax的学习笔记