Good afternoon
I have a question, currently I require a method that passes an arrayList of values, it is necessary to compare it with an enum and in case some of the values in the list are not found in the enum, it returns false, this as a validation method, I'm thinking about how to do it, I would like to do it via stream but I can't think of any ideas.
My ArrayList is like the following:
List<String> listOfService = new ArrayList<>(Arrays.asList("mobile_postpaid", "mobile_internet", "sam"));
My EnumClass
package org.tmve.customer.ms.beans.response;
import lombok.Getter;
@Getter
public enum Service {
MOBILE_PREPAID("mobile_prepaid"), MOBILE_POSTPAID("mobile_postpaid"), VALUE_ADDED_SERVICE("value_added_service"), MOBILE_INTERNET("mobile_internet"), DTH("dth"), LANDLINE("landline"), AUTHENTICATION("authentication");
private String value;
Service(String value) {
this.value = value;
}
}
As we can see in ListOfService comes the value of sam, which is not within the ENUM, so I would like a comparison method and in this case where sam is not found within the values of enum Service, return false
You can either add a method to your enum
public List<String> getValues() {
return values().map(service -> service.value);
}
And then use allMatch
like this
boolean b = listOfService.stream()
.allMatch(service -> Service.getValues().contains(service))
see allMatch doc
You can probably optimize the enum method by storing the list or using another comparison