当前位置:网站首页>List delete collection elements
List delete collection elements
2022-07-27 08:25:00 【Refuge in Beihai】
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Student {
private String name;
private String age;
}
public static void main(String[] args) {
iteratorDelete();
// listDelete();
}
/** * list.remove() Delete will report an error */
private static void listDelete() {
List<Student> cityList = setValue();
System.out.println(cityList.size());
for (Student student : cityList) {
if(student.getAge().equals("xx")){
cityList.remove(student);
}
}
System.out.println(cityList.size());
}
/** * list Delete data delete using iterator */
private static void iteratorDelete() {
List<Student> cityList = setValue();
ListIterator<Student> iterator =cityList.listIterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if(student.getAge().equals("xx")){
System.out.println(" What is deleted is the data of the collection , Yes iterator No impact ");
iterator.remove();
}
System.out.println(" Length after deletion :"+cityList.size());
int i = iterator.nextIndex();
int previousIndex = iterator.previousIndex();
System.out.println(student + "==" + i + "==" + previousIndex);
}
System.out.println(cityList.size());
}
private static List<Student> setValue(){
List<Student> cityList = new ArrayList<Student>();
cityList.add(new Student("xx", "xx"));
cityList.add(new Student("qq", "qq"));
cityList.add(new Student("ww", "ww"));
cityList.add(new Student("ee", "ee"));
cityList.add(new Student("rr", "rr"));
cityList.add(new Student("tt", "tt"));
return cityList;
}
边栏推荐
- 【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
- 阿里云国际版回执消息简介与配置流程
- The seta 2020 international academic conference will be held soon. Welcome to attend!
- "PHP Basics" tags in PHP
- Iterators and generators
- Demo:st05 find text ID information
- I drew a Gu ailing with characters!
- containerd拉取私库镜像失败(kubelet)
- All in one 1353 -- expression bracket matching (stack)
- JS basic knowledge - daily learning summary ①
猜你喜欢

Translation character '/b' in C #

数据提取1

The seta 2020 international academic conference will be held soon. Welcome to attend!

Notes in "PHP Basics" PHP

Design and development of GUI programming for fixed-point one click query

Process control - Branch
![[NPUCTF2020]ReadlezPHP 1](/img/d9/590446b45f917be3f077a9ea739c20.png)
[NPUCTF2020]ReadlezPHP 1

【目标检测】YOLOv6理论解读+实践测试VisDrone数据集

Solution to the program design of the sequence structure of one book (Chapter 1)

IBM3650M4实体机安装VCenter7.0
随机推荐
All in one 1353 -- expression bracket matching (stack)
[applet] how to get wechat applet code upload key?
Why do major domestic manufacturers regard cloud computing as a pastry? Do you really understand this trillion market
UVM入门实验1
Qt Creator代码风格插件Beautifier
如何在qsim查看软件对象的实例?
Mqtt instruction send receive request subscription
Data extraction 2
[ciscn2019 southeast China division]web11 1
First experience of tryme in opengauss
Prevent cookies from modifying ID to cheat login
Notes in "PHP Basics" PHP
Dormitory access control system made by imitating the boss (III)
Local Oracle reported ora-12514: tns: the listener cannot recognize the requested service at present
2020 International Machine Translation Competition: Volcano translation won five championships
Graph node deployment and testing
Stored procedure test 1 -- first acquaintance of love
Node installation and debugging
Help send some recruitment. If you are interested, you can have a look
containerd拉取私库镜像失败(kubelet)