Need to remove all tariffs from plans if boolean is false. How to write code to remove it all? I tried using removeAll, but doesn't work. And maybe it is possible to simplify this code? Just need to check if the plan has tariffs with N and M code. If don't have or have only one code ( M or N ), then delete all tariffs.
List < Plan > plan = new ArrayList < Plan > ();
plan.addAll(plans2);
boolean t = false;
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")) {
for (int jj = 0; jj < tariff.size(); jj++) {
if (tariff.get(jj).getCode().equals("M")) {
t = true;
}
}
}
}
//How to remove here?
if (!t) {
//plan.get(i).getTariff().remove();
}
}
Here's a simplified (assuming you're comfortable with lambda functions and streams) implementation: