当前位置:网站首页>List集合数据移除(List.subList.clear)
List集合数据移除(List.subList.clear)
2022-07-06 09:33:00 【小白说(๑• . •๑)】
List集合数据移除(List.subList.clear)
这两天遇到这么一个问题:一个集合的数据当作参数传入,现在分段使用这个集合的数据,使用完的数据需要在这个集合中被移除,方便获取第二段的数据。
假设:一个集合的数据长度是11个,现分两段使用这个集合,第一段使用5个数据,第二段使用6个数据,达到分段使用集合的数据。为了能正确的获取到数据,获取完成第一段数据之后,将前五个数据从list集合中移除,然后在获取第二段数据,这样也方便循环。
最开始我是这样做的remove(),移除数据:
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");
// 模拟获取第一段前五个数
List<String> oneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
oneList.add(list.get(i));
list.remove(i);
}
// 模拟获取第二段后6个数
List<String> twoList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
twoList.add(list.get(i));
list.remove(i);
}
}
因为ArrayList是线程不安全的,在循环中这样做很明显不合适,并发情况下很可能会出现问题。
所有我换了另外一种方法,removeAll(),移除List中的数据:
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");
// 模拟获取第一段前五个数
List<String> oneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
oneList.add(list.get(i));
}
list.removeAll(oneList);
// 模拟获取第二段后6个数
List<String> twoList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
twoList.add(list.get(i));
}
list.removeAll(twoList);
}
注意集合中的数据发生了变化,这也是我后来才发现的变化:
list原本有11个数据,取出5个之后应该还剩下6个数据,但是removeAll之后还剩下3个数据,这样肯定是不对的,因为这会让获取第二段数据的时候出现数组下标越界的异常。
后来看源码才发现,removeAll移除的是相同的元素。
查资料发现使用迭代器不会移除重复的元素,不过迭代器移除数据不是我想要的方式,也达不到我想要的效果。
最后发现使用subList()获取当前集合的子集去移除是靠谱的,并且没有出现上面两种问题。
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");
// 模拟获取第一段前五个数
List<String> oneList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
oneList.add(list.get(i));
}
// 获取子集清除数据
List<String> subList_1 = list.subList(0, 5);
subList_1.clear();
// 模拟获取第二段后6个数
List<String> twoList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
twoList.add(list.get(i));
}
list.subList(0, 6).clear();
}
在最后要说明一下subList作用和参数:
subList是获取当前集合的子集的方法
参数fromIndex:指定新列表的起始点(包括该点)
参数toIndex:指定新列表的终点(不包括该点)
个人理解为从下标fromIndex开始取值,取toIndex个值。
参考连接
边栏推荐
- Interview collection library
- ByteDance overseas technical team won the championship again: HD video coding has won the first place in 17 items
- 吴军三部曲见识(五) 拒绝伪工作者
- Resume of a microservice architecture teacher with 10 years of work experience
- README. txt
- mysql的列的数据类型详解
- 面试集锦库
- Flink 解析(七):时间窗口
- 暑假刷题嗷嗷嗷嗷
- TCP's three handshakes and four waves
猜你喜欢

ByteDance technical Interviewer: what kind of candidate do I want to pick most

Activiti目录(四)查询代办/已办、审核

redux使用说明

吴军三部曲见识(四) 大家智慧

was unable to send heartbeat

Some feelings of brushing leetcode 300+ questions

Mongodb learning notes

Idea resolving jar package conflicts

Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.

Flink源码解读(二):JobGraph源码解读
随机推荐
MySQL optimization notes
Flink源码解读(一):StreamGraph源码解读
Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
Mongodb learning notes
@RestController、@Controller
Compile homework after class
字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」
GCC error: terminate called after throwing an instance of 'std:: regex_ error‘ what(): regex
8086 分段技术
On the clever use of stream and map
DOS function call
@RequestMapping、@GetMapping
SQL调优小记
ByteDance overseas technical team won the championship again: HD video coding has won the first place in 17 items
Conception du système de thermomètre numérique DS18B20
服务器端渲染(SSR)和客户端渲染(CSR)的区别
Activiti目录(四)查询代办/已办、审核
Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
Shell_ 01_ data processing
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games