//查询所有的销售人员
EpdSalesperson epdSalesperson = new EpdSalesperson();
List<EpdSalesperson> epdSalespeople = epdSalespersonService.selectEpdSalespersonList(epdSalesperson);
//查询拜访计划
EpdVisitPlanCustomer epdVisitPlanCustomer = new EpdVisitPlanCustomer();
epdVisitPlanCustomer.setCustomerId(id);
List<EpdVisitPlanCustomer> epdVisitPlanCustomers = epdVisitPlanCustomerService.selectEpdVisitPlanCustomerList(epdVisitPlanCustomer);
//获取拜访计划中销售人员的id
List<String> collect = epdVisitPlanCustomers.stream().map(e -> e.getPersonId()).collect(Collectors.toList());
//第一种解决方式
//遍历所有的销售人员 如果拜访计划中的销售人员的id包含,则删除
Iterator<EpdSalesperson> iterator = epdSalespeople.iterator();
while(iterator.hasNext()){
EpdSalesperson next = iterator.next();
if(collect.contains(next.getId())){
iterator.remove();
}
}
//第二种解决方式
epdSalespeople.removeIf(next -> collect .contains(next.getId()));
//第三种方式
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> filteredList = list.stream()
.filter(num -> num != 2 && num != 4)
.collect(Collectors.toList());
System.out.println(filteredList); // [1, 3, 5]
}
下面一种写法
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author yangquan
* @since 2023-11-15
*/
public class Test11 {
public static void main(String[] args) {
List<Personn> list1 = Arrays.asList(new Personn("Alice", 25),new Personn("Bob", 31),new Personn("Charlie", 35), new Personn("Bob", 30));
List<Personn> list2 = Arrays.asList(new Personn("Alice", 25), new Personn("Charlie", 35), new Personn("Dave", 40));
List<Personn> list3 = new ArrayList<>(list1);
// Set<Personn> set2 = new HashSet<>(list2);
list3.removeIf(next -> list2.stream().anyMatch(e -> e.getName().equals(next.getName()) && e.getAge() == next.getAge()));
//也可以将list2转换为一个HashSet。这是因为HashSet的查找操作通常比ArrayList更快,这可以提高你的代码效率
//Set<Personn> set2 = new HashSet<>(list2);
//list3.removeIf(next -> set2.stream().anyMatch(e -> e.getName().equals(next.getName()) && e.getAge() == next.getAge()));
System.out.println(list3+" OPOP");
}
}
class Personn {
private String name;
private int age;
public Personn(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Personn{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
执行结果
[Personn{name='Bob', age=31}, Personn{name='Bob', age=30}] OPOP
直接上代码
public static void main(String[] args) throws InterruptedException {
// List<String> strings = Arrays.asList("1", "2", "3", "4", "5","5");
List<String> strings = new ArrayList<>();
strings.add("1");
strings.add("2");
strings.add("3");
strings.add("4");
strings.add("5"); // 要删除的元素
strings.add("5");
第一种 错误
for (String string : strings) {
if("1".equals(string)){
strings.remove(string);
}
}
第二种 错误 因为删掉了下表为4的,下标为5的就往前移动变成了4,下次遍历不到他
for (int i = 0; i < strings.size(); i++) {
if("5".equals(strings.get(i))){
strings.remove(i);
}
}
第三种 正确
Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()){
if("5".equals(iterator.next())){
iterator.remove();
}
}
第四种 正确
strings.removeIf(e -> e.equals("5"));
System.out.println(strings+" +++");
// List<String> list = new ArrayList<>();
// list.add("1");
// list.add("2");
// list.add("3");
// list.add("4");
// list.add("5"); // 要删除的元素
// list.add("6");
//
// // 使用removeIf方法删除所有等于"5"的元素
// list.removeIf(e -> e.equals("5"));
// list.forEach(System.out::println);
}
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!!