0

I have

public enum Brand{
BMW,AUDI,FIAT;
}

private List<String> initializeCars() {
car= new ArrayList<>();
    car.add("CHRYSLER");
    car.add("BMW");
    car.add("AUDI");
    car.add("SKODA");
    car.add("FIAT");
    car.add("PORSCHE");
    return car;

I need method which will return List<String> of cars which are on car list but not in enum.

I tried

public List<String> extractInvalidCars(List<String> cars) {
List<String> enumValues = new ArrayList<>();
for (ProductCategory enumBrand: Brand.values()) {
enumValues.add(String.valueOf(enumBrand));
}
for (String s : cars) {
        for (String enumValue : enumValues) {
            if (enumValue.equals(s)) {
                enumValues.remove(s);
            }
        }
    }
  return enumValues;
}

But I got ConcurrentModificationException

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Mannual
  • 3
  • 3

1 Answers1

1

the error means you are iterating and modifying a colletion at the same time... that is not correct, on the other hand, dont mix types... enums are enums and strings are strings

my suggestion

. convert the enum to a list of strings . do a intersection between cars and the new list of string_enums

example:

List<String> list = Stream.of(Brand.values()).
     map(Brand::toString).collect(Collectors.toList());

then you can do

cars.removeAll(list);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97