当前位置:网站首页>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
));边栏推荐
- Day06 homework -- skill question 2
- TCP solves the problem of short write
- [leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
- Probability model in machine learning
- Horizontal comparison of the data of the top ten blue chip NFTs in the past half year
- Espressif plays with the compilation environment
- 2022化工自动化控制仪表操作证考试题模拟考试平台操作
- Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world
- CF1481C Fence Painting
- 2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
猜你喜欢

Original root and NTT 5000 word explanation

Review notes of Microcomputer Principles -- zoufengxing

Datax的学习笔记

Node-v download and application, ES6 module import and export

谷粒学院的全部学习源码

围棋智能机器人阿法狗,阿尔法狗机器人围棋

The child and binary tree- open root inversion of polynomials

STM32+MFRC522完成IC卡号读取、密码修改、数据读写

NFT与数字藏品到底有何区别?

209. Subarray with the smallest length
随机推荐
PHP page value transfer
JDBC database connection pool (Druid Technology)
PHP和MySQL获取week值不一致的处理
redis原理和使用-基本特性
对标注文件夹进行清洗
【LeetCode数据库1050】合作过至少三次的演员和导演(简单题)
How to quickly learn a programming language
839. 模拟堆
堆外内存的使用
2B和2C
CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
Learning notes of automatic control principle - Performance Analysis of continuous time system
原根与NTT 五千字详解
Nuxt - 项目打包部署及上线到服务器流程(SSR 服务端渲染)
209. Subarray with the smallest length
巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
Grain College of all learning source code
(2006,Mysql Server has gone away)问题处理
PAT 甲级 A1076 Forwards on Weibo
Clean the label folder