配列の列挙にアクセスする方法

ジェニー:

私は、製品の2セットを持っています

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)でエラーが出ます

ユージン:

あなたの列挙型は変更されませんので、あなたが構築できる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);
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=208113&siteId=1