当前位置:网站首页>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
));边栏推荐
- MySQL strengthen knowledge points
- PHP page value transfer
- ext3文件系统的一个目录下,无法创建子文件夹,但可以创建文件
- [eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
- (1) CTS tradefed test framework environment construction
- at、crontab
- ES6 modular import and export) (realize page nesting)
- 数据库操作 题目二
- Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
- 【无标题】
猜你喜欢

语音聊天app源码——钠斯直播系统源码

网络安全漫山遍野的高大上名词之后的攻防策略本质

C# Serialport的发送和接收

Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points

Polynomial open root

Unity topdown character movement control

Clean the label folder

CF1481C Fence Painting

CF1481C Fence Painting

Database operation topic 2
随机推荐
jvm命令归纳
数据库操作 技能6
The essence of attack and defense strategy behind the noun of network security
day06 作业--技能题2
Self review ideas of probability theory
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
838. 堆排序
(1) CTS tradefed test framework environment construction
Nuxt - 项目打包部署及上线到服务器流程(SSR 服务端渲染)
redis原理和使用-安装和分布式配置
原根与NTT 五千字详解
js闭包:函数和其词法环境的绑定
tornado之多进程服务
JDBC database connection pool (Druid Technology)
Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
Numpy Foundation
Day 6 summary & database operation
NTT(快速数论变换)多项式求逆 一千五百字解析
CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
Overview of motion recognition evaluation