java双重for循环取值及删除重复的元素 集合删除,集合遍历

		//查询所有的销售人员
 		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);
    }

这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值