我有两套产品
public enum ProductType {
FOUNDATION_OR_PAYMENT ("946", "949", "966"),
NOVA_L_S_OR_SESAM ("907", "222");
private String[] type;
ProductType(String... type) {
this.type = type;
}
}
然后给定值“ actualProductType”,我需要检查它是否是productType的一部分..我该如何做..
isAnyProductTypes(requestData.getProductType(), ProductType.NOVA_L_S_SESAM)
public boolean isAnyProductTypes(String actualProductType, ProductType productTypes) {
return Arrays.stream(productTypes).anyMatch(productType -> productType.equals(actualProductType));
}
我在这部分Arrays.stream(productTypes)遇到错误
答案 0 :(得分:2)
由于您的枚举没有改变,因此可以在其中建立Map
以便快速查找:
public enum ProductType {
FOUNDATION_OR_PAYMENT("946", "949", "966"),
NOVA_L_S_OR_SESAM("907", "222");
static Map<String, ProductType> MAP;
static {
MAP = Arrays.stream(ProductType.values())
.flatMap(x -> Arrays.stream(x.type)
.map(y -> new SimpleEntry<>(x, y)))
.collect(Collectors.toMap(Entry::getValue, Entry::getKey));
}
private String[] type;
ProductType(String... type) {
this.type = type;
}
public boolean isAnyProductTypes(String actualProductType, ProductType productTypes) {
return Optional.ofNullable(MAP.get(actualProductType))
.map(productTypes::equals)
.orElse(false);
}
}
答案 1 :(得分:1)
您应该将类型更改为Set<String>
,并将构造函数也更改为
ProductType(String... type) {
this.type = new HashSet<>(Arrays.asList(type));
}
查找将非常简单
return productType.getType().contains(requestData.getProductType())