300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 从List集合中删除指定元素

从List集合中删除指定元素

时间:2020-01-13 10:14:31

相关推荐

从List集合中删除指定元素

从List集合中删除指定元素的几种方式

最近在使用这个操作的时候搜索了一下网上的文章,发现有些能用有些不能用,由于使用之前没有做详细的测试,导致程序bug,今天抽空做了一下总结和测试。

package com.arthas.testDemo.demo;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import java.util.stream.Collectors;import java.util.stream.IntStream;/*** @date /8/24 15:15*/public class ListRemove {class User {public String name;public Integer age;public String classNo;@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +", classNo='" + classNo + '\'' +'}';}}public List<User> getUserList(int num) {List<User> list = new ArrayList<>();for (int i = 0; i < num; i++) {User u = new User();u.age = i + 10;u.name = "a" + i;u.classNo = String.valueOf(i);list.add(u);}return list;}public static void main(String[] args) {ListRemove listRemove = new ListRemove();//数据生成List<User> userList = listRemove.getUserList(5);System.out.println("原数据:" + Arrays.toString(userList.toArray()));//测试过滤掉 name = a2 或者 classNo = 4//方法一:Iterator删除List<User> iteratorList = new ArrayList<>(userList);Iterator<User> iterator = iteratorList.iterator();while (iterator.hasNext()) {User next = iterator.next();if ("a2".equals(next.name) || "4".equals(next.classNo)) {iterator.remove();}}System.out.println("Iterator删除:" + Arrays.toString(iteratorList.toArray()));//方法二:removeIf删除List<User> removeIfList = new ArrayList<>(userList);removeIfList.removeIf(next -> "a2".equals(next.name) || "4".equals(next.classNo));System.out.println("removeIf删除:" + Arrays.toString(removeIfList.toArray()));//方法三:使用stream 注意:此方法测试未达到预期结果, 不适用List<User> streamList = new ArrayList<>(userList);streamList.stream().findFirst().map(e -> {if ("a2".equals(e.name) || "4".equals(e.classNo)) {streamList.remove(e);}return e;});System.out.println("stream删除:" + Arrays.toString(streamList.toArray()));//方法四:使用stream+索引 注意:此方法测试未达到预期结果,不适用List<User> streamIndexList = new ArrayList<>(userList);IntStream.range(0, streamIndexList.size()).filter(i ->"a2".equals(streamIndexList.get(i).name) || "4".equals(streamIndexList.get(i).classNo)).boxed().findFirst().map(i -> streamIndexList.remove((int) i));System.out.println("stream+index删除:" + Arrays.toString(streamIndexList.toArray()));//方法三:使用filterList<User> filterList = new ArrayList<>(userList);filterList = filterList.stream().filter(e -> !"a2".equals(e.name) && !"4".equals(e.classNo)).collect(Collectors.toList());System.out.println("filter删除:" + Arrays.toString(filterList.toArray()));System.out.println("原数据:" + Arrays.toString(userList.toArray()));}}

生成结果如下:

原数据:[User{name='a0', age=10, classNo='0'}, User{name='a1', age=11, classNo='1'}, User{name='a2', age=12, classNo='2'}, User{name='a3', age=13, classNo='3'}, User{name='a4', age=14, classNo='4'}]Iterator删除:[User{name='a0', age=10, classNo='0'}, User{name='a1', age=11, classNo='1'}, User{name='a3', age=13, classNo='3'}]removeIf删除:[User{name='a0', age=10, classNo='0'}, User{name='a1', age=11, classNo='1'}, User{name='a3', age=13, classNo='3'}]stream删除:[User{name='a0', age=10, classNo='0'}, User{name='a1', age=11, classNo='1'}, User{name='a2', age=12, classNo='2'}, User{name='a3', age=13, classNo='3'}, User{name='a4', age=14, classNo='4'}]stream+index删除:[User{name='a0', age=10, classNo='0'}, User{name='a1', age=11, classNo='1'}, User{name='a3', age=13, classNo='3'}, User{name='a4', age=14, classNo='4'}]filter删除:[User{name='a0', age=10, classNo='0'}, User{name='a1', age=11, classNo='1'}, User{name='a3', age=13, classNo='3'}]原数据:[User{name='a0', age=10, classNo='0'}, User{name='a1', age=11, classNo='1'}, User{name='a2', age=12, classNo='2'}, User{name='a3', age=13, classNo='3'}, User{name='a4', age=14, classNo='4'}]Process finished with exit code 0

测试结果表明,使用Iterator, removeIf, filter均能正常实现这个功能,而网上多篇文章提供的strem.findFirst操作,stream+索引操作在测试中没有达到预期结果;不知道当时的作者在完成文章时是如何实现预期结果的,仓促完成,不足之处请指正!谢谢!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。