2

I have two sets of Products

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;
    }
}

Then given a value"actualProductType" I need to check if it is part of the productType ..How do I do this ..

isAnyProductTypes(requestData.getProductType(), ProductType.NOVA_L_S_SESAM)

 public boolean isAnyProductTypes(String actualProductType, ProductType productTypes) {
        return Arrays.stream(productTypes).anyMatch(productType -> productType.equals(actualProductType));
    }

I get an error at this part Arrays.stream(productTypes)

1
  • 1
    Something is off with the terminology here. Is a product type a String or an array of strings? They can't both be "product type" Commented May 3, 2019 at 13:52

2 Answers 2

2

Since your enum does not change, you could build a Map inside it for faster look-up:

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

you should change the type to Set<String> and the constructor as well

ProductType(String... type) {
    this.type = new HashSet<>(Arrays.asList(type));
}

and the lookup will be very simple

return productType.getType().contains(requestData.getProductType())

5 Comments

not really what the question meant I would say. this changes the contract of the expected api the user has shared.
@Naman it solves the problem. The OP did not state that he can't modify the enum, it's certainly better than doing the lookup every time and it's simplest solution ... no need for stream really
storing String[] as Set<String> wouldn't solve the problem is what I wanted to point out. It's just an attribute attached with each type. To check none of the types has a value, you would still have to iterate through all such types and check amongst there sets if the value is contained.
@Naman if you look at the isAnyProductTypes signature you'll see that it receives a single ProductType so he wants to search inside a given enum value and not all
well I would say the question is unclear in that aspect then for the type, productTypes it reads..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.