Kashyap :
I have below enum
public enum ApiGroup {
H("gp"),
S_CARDS("cards"),
S_CARDMETADATA("cardmetadata"),
S_PRODUCTS("products");
private String value;
private ApiGroupType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
private static final List<String> sApiGroups = Arrays
.asList(ApiGroupType.S_CARDS.value, ApiGroupType.S_CARDMETADATA.value, ApiGroupType.S_PRODUCTS.value );
public static List<String> getSApiGroupsList() {
return sApiGroups;
}
Currently, I am fetching the required values via creating List. If any future addition is required to the list, there is a need to add the required values first as enum, then in the list, is there any better way this addition at 2 places can be avoided. By adding value at single place should work.
Kartik :
Change your method to the following and delete the sApiGroups
static list.
public static List<String> getSApiGroupsList() {
return Arrays.stream(values())
.filter(apiGroup -> apiGroup.name().startsWith("S_"))
.map(ApiGroup::getValue)
.collect(Collectors.toList());
}
Learn more about the values()
method in the docs.
Edit
As per @ernest_k's comment, we don't need to stream for every call of this method. So can just have:
private static final List<String> sApiGroups = Arrays.stream(values())
.filter(apiGroup -> apiGroup.name().startsWith("S_"))
.map(ApiGroup::getValue)
.collect(Collectors.toList());
public static List<String> getSApiGroupsList() {
return sApiGroups;
}