当前位置:网站首页>List set data removal (list.sublist.clear)
List set data removal (list.sublist.clear)
2022-07-06 17:18:00 【Xiaobai said (๑• ๑)】
List Set data removal (List.subList.clear)
I have encountered such a problem these two days : A set of data is passed in as a parameter , Now? piecewise
Use the data of this set , The used data needs to be removed from this set , It is convenient to obtain the data of the second paragraph .
hypothesis : The data length of a set is 11 individual , Now use this set in two paragraphs , The first paragraph uses 5 Data , Use of the second paragraph 6 Data , Achieve segmented use of set data . In order to get the data correctly , After obtaining the first data , Take the first five data from list Remove... From collection , Then get the second data , This also facilitates circulation .
At first I did this remove()
, Remove data :
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("g");
list.add("k");
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
list.add("h");
list.add("i");
// Simulate to get the first five numbers in the first paragraph
List<String> oneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
oneList.add(list.get(i));
list.remove(i);
}
// After the simulation obtains the second paragraph 6 Number
List<String> twoList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
twoList.add(list.get(i));
list.remove(i);
}
}
because ArrayList It's not thread safe , It is obviously not appropriate to do this in the cycle , Problems are likely to occur in concurrency .
So I changed another way ,removeAll()
, remove List Data in :
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("g");
list.add("k");
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("a");
list.add("a");
list.add("a");
// Simulate to get the first five numbers in the first paragraph
List<String> oneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
oneList.add(list.get(i));
}
list.removeAll(oneList);
// After the simulation obtains the second paragraph 6 Number
List<String> twoList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
twoList.add(list.get(i));
}
list.removeAll(twoList);
}
Notice that the data in the set has changed , This is also the change I found later :
list Originally 11 Data , Take out 5 After that, there should be still 6 Data , however removeAll After that, there is still 3 Data , This must be wrong , Because this will make the array subscript out of bounds exception when obtaining the second segment of data .
Later, I found out by looking at the source code ,removeAll The same elements are removed .
Looking up the data, we found that using iterators will not remove duplicate elements , But the iterator is not the way I want to remove data , It can't achieve the effect I want .
Finally, we found that using subList()
It is reliable to get a subset of the current set to remove , And there are no above two problems .
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("g");
list.add("k");
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("a");
list.add("a");
list.add("a");
// Simulate to get the first five numbers in the first paragraph
List<String> oneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
oneList.add(list.get(i));
}
// Get subset clear data
List<String> subList_1 = list.subList(0, 5);
subList_1.clear();
// After the simulation obtains the second paragraph 6 Number
List<String> twoList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
twoList.add(list.get(i));
}
list.subList(0, 6).clear();
}
At the end of the article, I want to explain subList Actions and parameters :
subList Is the method to get a subset of the current set
Parameters fromIndex: Specify the starting point of the new list ( Include this point )
Parameters toIndex: Specify the end point of the new list ( This point is not included )
Personally, it means subscript fromIndex Start taking value , take toIndex It's worth .
Refer to the connection
边栏推荐
- Only learning C can live up to expectations top2 P1 variable
- 案例:检查空字段【注解+反射+自定义异常】
- Control transfer instruction
- ByteDance technical Interviewer: what kind of candidate do I want to pick most
- Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here
- 8086 segmentation technology
- 8086 分段技术
- TCP的三次握手和四次挥手
- Wu Jun trilogy insight (IV) everyone's wisdom
- Use of mongodb in node
猜你喜欢
Akamai浅谈风控原理与解决方案
汇编语言段定义
Some feelings of brushing leetcode 300+ questions
Introduction to spring trick of ByteDance: senior students, senior students, senior students, and the author "brocade bag"
Activiti directory (V) reject, restart and cancel process
肖申克的救赎有感
Install docker under windows10 (through Oracle VM VirtualBox)
Prototype chain inheritance
原型链继承
JVM之垃圾回收器上篇
随机推荐
JVM 垃圾回收器之Garbage First
List集合数据移除(List.subList.clear)
js垃圾回收机制和内存泄漏
控制转移指令
Akamai 反混淆篇
CTF逆向入门题——掷骰子
复盘网鼎杯Re-Signal Writeup
Flink源码解读(三):ExecutionGraph源码解读
Control transfer instruction
一个数10年工作经验的微服务架构老师的简历
Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021
Only learning C can live up to expectations top5 S1E8 | S1E9: characters and strings & arithmetic operators
MySQL数字函数
Flink parsing (VI): savepoints
逻辑运算指令
Description of project structure configuration of idea
Some feelings of brushing leetcode 300+ questions
唯有学C不负众望 TOP3 Demo练习
汇编课后作业
Mongodb在node中的使用