当前位置:网站首页>Problems caused by List getting the difference
Problems caused by List getting the difference
2022-07-30 03:43:00 【Peipei Dad】
Background
When saving data in batches, it needs to be compared with the database.If the current data exists, it will be removed from the List collection.
If the List is deleted in batches, removeAll is generally considered.
The operation process is as follows:
1. Match the external unique field of the existing batch data with the data in the database, and return the existing List object
2. Use the removeAll function to delete in batches to obtain the current difference.
After using the removeAll function, data duplication is directly caused when saving in batches.
Api description
Operation Type | Method | Description |
Intersection | listA.retainAll(listB) | After calling the method, ListA becomes the intersection of the two sets, and ListB remains unchanged |
Difference | listA.removeAll(listB) | After calling the method, ListA becomes the difference of the two sets, and ListB remains unchanged |
Union | 1.listA.removeAll(listB) 2.listA.addAll(listB) | To remove duplicates, first take the difference and then the union.ListA becomes the union of the two sets, ListB remains the same |
removeAll source code
Saying anything is false, look at the removeAll source code brand.
public boolean removeAll(Collection> c) {return batchRemove(c, false, 0, size);}...boolean batchRemove(Collection> c, boolean complement,final int from, final int end) {Objects.requireNonNull(c);final Object[] es = elementData;int r;// Optimize for initial run of survivorsfor (r = from;; r++) {if (r == end)return false;if (c.contains(es[r]) != complement)break;}int w = r++;try {for (Object e; r < end; r++)if (c.contains(e = es[r]) == complement)es[w++] = e;} catch (Throwable ex) {// Preserve behavioral compatibility with AbstractCollection,// even if c.contains() throws.System.arraycopy(es, r, es, w, end - r);w += end - r;throw ex;} finally {modCount += end - w;shiftTailOverGap(es, w, end);}return true;}
As we can see, each object needs to be compared in a loop.
for (r = from;; r++) {if (r == end)return false;if (c.contains(es[r]) != complement)break;}
The data found from the database contains other fields such as ID.In this way, the properties of the two objects are different.So it will return false.This defeats the purpose of weight loss.
Custom objects can be deduplicated using the Stream method of JDK8+ below.
Match the data already in the library (exitList) with the data that needs to be saved (entityList), and pick out the data that does not exist in the library and put it into a new List (saveDataEntityList).
The pseudo code is as follows:
saveDataEntityList = entityList.stream().filter(f -> !exitList.stream().map(Entity::getId).collect(Collectors.toList()).contains(f.getId())).collect(Collectors.toList());
After testing, found OK, no duplicate data.
Summary
removeAll is suitable for subset exact matching and basic type operations. It is recommended that when customizing objects, do not use the removeAll method, but use the stream method.
边栏推荐
猜你喜欢
Answer these 3 interview questions correctly, and the salary will go up by 20K
Public chains challenging the "Impossible Triangle"
Process priority nice
分类之决策树分类
Software testing interview questions and answer analysis, the strongest version in 2022
Rpc 和 gRpc 简介汇总
星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
MyCat中对分库分表、ER表、全局表、分片规则、全局序列等的实现与基本使用操作
智能答题功能,CRMEB知识付费系统必须有!
传输层详解
随机推荐
LoadBalancer load balancing
Record NLP various resource URLs
【科研工具的使用】A
精品:淘宝/天猫获取购买到的商品订单详情 API
淘宝/天猫获得淘宝店铺详情 API
Is the snowflake the same question?
Nacos namespace
历经5面的阿里实习面经篇~
基于全志D1-H和XR806的名贵植物监控装置
gnss rtcm rtklib Ntrip...
2022-07-29 第四小组 修身课 学习笔记(every day)
Redis (ten) - Redission principle and practice
Sentinel Traffic Guard
淘宝/天猫获取卖出的商品订单列表 API
leetcode 5 questions a day-Day01
The relationship between the number of Oracle processes and the number of sessions
WPF 学习笔记《WPF布局基础》
NLP自然语言处理(二)
解决编译安装gdb-10.1 unistd.h:663:3: error: #error “Please include config.h first.“ 问题
WPF递归获取窗体中指定控件类型列表