0

Can have few plans and few tariffs. Need to remove all tariffs from plan, when remove == N. Tried to do it with this code, but nothing happens. I mean, I debugged and saw in console, that remove is T, but code don't remove tariffs anyway. How to fix removing?

    remove = "Y";
    List<Plan> plan = new ArrayList<Plan>();
    plan.addAll(plan2);
    
    for (int i = 0; i < plan.size(); i++) { 
        List<Tariff> tariff = new ArrayList<Tariff>();
        tariff.addAll(plan.get(i).getTariff());

        for (int j = 0; j < tariff.size(); j++) {  
            if(tariff.get(j).getCode().equals("N")) {
                remove = "N";
            }
        }
        plan.get(i).getTariff().removeIf(condition -> remove.equals("Y"));
    }
Deividas.r
  • 17
  • 1

1 Answers1

0

If it is needed to remove all tariffs from a plan when any tariff has a code == N, this may be done as follows:

List<Plan> cleaned = new ArrayList<>(plan2);

cleaned.stream()
    .filter(p -> p.getTariff().stream().anyMatch(t -> "N".equals(t.getCode())))
    .forEach(p -> p.getTariff().clear());
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42