最近在使用这个操作的时候搜索了一下网上的文章,发现有些能用有些不能用,
由于使用之前没有做详细的测试,导致程序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 2021/8/24 15:15
*/
public class ListRemove {
class User {
public String name;
public Integer age;
public String classNo;
@Override
public 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()));
//方法三:使用filter
List<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