我有两套产品
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 ..How的一部分,我要这样做吗..
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) )有一个错误。
发布于 2019-05-03 13:41:29
由于枚举没有改变,您可以在其中构建一个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);
}
}发布于 2019-05-03 13:50:14
您应该将类型更改为Set<String>和构造函数。
ProductType(String... type) {
this.type = new HashSet<>(Arrays.asList(type));
}而且查找将非常简单
return productType.getType().contains(requestData.getProductType())https://stackoverflow.com/questions/55970915
复制相似问题