Home > Back-end >  Java remove all only when exists two codes in List
Java remove all only when exists two codes in List

Time:09-22

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();
    }
}

CodePudding user response:

Here's a simplified (assuming you're comfortable with lambda functions and streams) implementation:

public void someMethod() {
   ...
   plans.forEach(p -> cleanTariffs(p));  // replace the outer for loop
   ...
}

private void cleanTariffs(Plan plan) {
    if (!containsAllRequiredCodes(plan.getTariffs())) {
        plan.getTariffs().clear();
    }
}

private boolean containsAllRequiredCodes(List<Tariff> tariffs) {
    return tariffs.stream()
            .map(Tariff::getCode)
            .collect(Collectors.toSet()) // map tariff codes to a Set<String> which provides O(1) look up
            .containsAll(REQUIRED_TARIFF_CODES);
}

private static final Set<String> REQUIRED_TARIFF_CODES = Set.of("M", "N");

CodePudding user response:

Need to remove all tariffs from plans if boolean is false.

I looked at your attempt and it appears you are doing some redundant operations. For example, your nested loop to get the code doesn't make sense. Nor does the implied logic that a code can have two different values at the same time.

But you might try something like this.

  • iterate thru your list of plans.
  • retrieve the list and remove the offending values from the tarriff list using removeIf
for (Plan p : plan) {
     p.getTariff().removeIf(tariff-> tariff.getCode().equals("N") || 
           tariff.getCode().equals("M"));
}

I may have gotten the condition for removal wrong so you may have to alter it. This removes the tarriff if the code is either N or M.

Note that if getCode() returned a list of codes and you needed to check for both N and M you could replace the remove statement with:

p.getTariff().removeIf(tariff-> 
              tariff.getCode().containsAll(List.of("N","M"));

It is important that getTariff returns the actual list and not a copy.

CodePudding user response:

Using Stream API:

plan.stream().forEach(p-> { List<Tariff> tariffList = p.getTariff();
                            if (tariffList.stream()
                                          .map(Tariff::getCode)
                                          .filter(code -> code.equals("M")||
                                                          code.equals("N"))
                                          .distinct()
                                          .count()==2)
                               tariffList.clear();
                          });
  • Related