当前位置:网站首页>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
));边栏推荐
猜你喜欢

数据库操作 题目一

Form form

Database operation topic 2

ES6 modular import and export) (realize page nesting)

Canal 的学习笔记

Day06 homework - skill question 6

CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?

Original root and NTT 5000 word explanation

Qtcreator reports an error: you need to set an executable in the custom run configuration

Polynomial open root
随机推荐
【ARKit、RealityKit】把图片转为3D模型
“No input file specified “问题的处理
Uploading pictures on Alibaba cloud OSS
The child and binary tree- open root inversion of polynomials
力扣链表题
CF1481C Fence Painting
839. 模拟堆
Datax的学习笔记
ES6 modular import and export) (realize page nesting)
Learning notes of automatic control principle --- linear discrete system
垂直搜索
codeforces dp合集
Numpy Foundation
Database operation topic 2
How to quickly learn a programming language
堆外内存的使用
JS closure: binding of functions to their lexical environment
pycharm 打开多个项目的两种小技巧
JS file import of node
Store a group of positive and negative numbers respectively, and count the number of 0 -- assembly language implementation