当前位置:网站首页>Pit record_ TreeSet custom sorting results in less data loss
Pit record_ TreeSet custom sorting results in less data loss
2022-06-26 10:34:00 【My name is 985】
Problem code
Customize Comparable
Set set = new TreeSet( Customize Comparable);
set.addAll(List); //List Originally there were 5 individual , result set Only 4 individual , as a result of List Of compareTo The results returned are the same , Lead to the following item Abandoned .
principle
By tracking TreeSet Source code discovery ,TreeSet Used in TreeMap To store objects , take TreeSet The element as Map Of key, Continue tracking TreeMap Of put Method source code when compared to compareTo The return value is 0 when ,key Value doesn't change ( It's actually an update key Corresponding value,TreeSet Of value It's meaningless ), That is, when there are multiple returns 0 situations TreeSet Only the first element will be placed in the , Give up all the rest , The source code is relatively simple , No post here .
summary : When using TreeSet Sorting time , If realized compareTo Method returns 0 Data loss will occur in the case of .
Solution :
- Method 1 : Just don't let duplicate sorts appear , If the actual business cannot be avoided, change the sorting method .
- Method 2 : Use jdk8 Of list.stream().distinct().sort( Sort rule ).collect(Collectors.toList());
Stream#distinct It's based on toString To remove the weight , Can be rewritten List Of the elements in toString Method . If sort identical , It will not appear, only the first element will be put , The following phenomena are all discarded
边栏推荐
- Use of exec series functions (EXECL, execlp, execle, execv, execvp)
- Oracle11g 启动数据库时报错 ORA-27154: post/wait create failed
- What is a botnet
- 创建对象的时候堆内存的分配
- 2. merge two ordered arrays
- 挖财商学院证券开户安全嘛?
- Establishment of smart dialogue platform for wechat official account
- Threading model in webrtc native
- Today's headline adaptation scheme code
- MySQL第八次作业
猜你喜欢

C中字符串基本操作

String constant pool, class constant pool, and runtime constant pool

Full introduction to flexboxlayout (Google official flexible implementation of flow layout control)

MySQL第八次作业

MySQL第十三次作业-事务管理

創建對象的時候堆內存的分配

Jar version conflict resolution

118. 杨辉三角

瑞萨电子面向物联网应用推出完整的智能传感器解决方案

MySQL seventh job - update data
随机推荐
P1296 whispers of cows (quick row + binary search)
Execute Lua script in redis
【LeetCode】59. Spiral matrix II
[binary search] 4 Find the median of two positive arrays
SwiftUI 开发经验之为离线优先的应用程序设计数据层
1. sum of two numbers (leetcode topic)
Under the double reduction, the amount of online education has plummeted. Share 12 interesting uses of webrtc
How do technicians send notifications?
Nested recyclerview in nestedscrollview automatically slides to the bottom after switching
How to start the learning journey of webrtc native cross platform development?
方法区里面有什么——class文件、class文件常量池、运行时常量池
挖财商学院证券开户安全嘛?
創建對象的時候堆內存的分配
量化投资学习——经典书籍介绍
Introduction to libmagic
工程数学概率论统计简明教程第二版复习大纲
3行3列整形二维数组,求对角之和
1. 两数之和(LeetCode题目)
How QT uses quazip to compress and decompress files
MySQL Chapter 4 Summary