2

I need to validate the value of a parameters passed as part of REST API. These parameters are a fixed set of values. I thought of using a map having parameter name as key and enum as value. So I can check if the value sent in REST API is one of the enum keys.

But I am not able to create a Map with String key and enum value in java, tried creating Map and then putting and enum as value, but it fails.

class Validation {
    enum Type {
        INTERNAL,
        EXTERNAL
    }; 
}

Map<String, Object> validationMap = new HashMap<String, Object>();
validationMap.put("type", Validation.Type);

This is throwing an error that type is not defined.

4
  • What are you actually trying to do? For example Validation.type.class would compile; but does this actually do what you want? Commented Dec 19, 2018 at 11:30
  • Make sure you follow the Java Naming Conventions: class and enum names always start with uppercase, i.e. type should be Type. Commented Dec 19, 2018 at 11:38
  • Do you need a Map? Will (correcting case) Validation.Type.valueOf() not work? Commented Dec 19, 2018 at 11:38
  • @AndyTurner : Validation.type.class solves compilation problem, but I don't know how i can create an instance back using the class. Commented Dec 20, 2018 at 11:27

3 Answers 3

6

This is probably what you're looking for, Map<String, Object> is changed to Map<String, Validation.type>:

Map<String, Validation.type> validationMap = new HashMap<String, Validation.type>();
validationMap.put("type", Validation.type.INTERNAL);
validationMap.put("type2", Validation.type.EXTERNAL);

Your original code would have worked, if you had changed Validation.type to Validation.type.INTERNAL for example, however your validationMap map allows the storage of any Object, so validationMap.put("type2", 123.123); would also have worked, which is unlikely to be something you want.

Sign up to request clarification or add additional context in comments.

1 Comment

I like to put enum as value not the properties of enum.
2

Is this what you need?

Map<String, List<Validation.type>> validationMap = new HashMap<>();
validationMap.put("type", 
   Arrays.asList(Validation.type.EXTERNAL,Validation.type.INTERNAL));

Comments

0

I believe Your code does not work because Validation.Type is not an instance (of an enum). When you put a value in your map, I believe you would want to put in an actual instance as a value. I suppose that is why the above answers suggest adding an actual instance to the Map such as Type.INTERNAL or using a class instance.

This code will run

    Map<String, Object> validationMap = new HashMap<String, Object>();
    // this works
    validationMap.put("type", Validation.Type.EXTERNAL);
    // as shown by:
    System.out.println(validationMap.toString());
    // here is an analogy:
    // make an instance of a class
    Validation validation = new Validation();
    // this works
    validationMap.put("classType",validation);
    Validation validationGet = (Validation) validationMap.get("classType");
    System.out.println(validationMap.toString());
    // by analogy this will not work because Validation is not an instance:
    // validationMap.put("classType",Validation);

And the output on my machine is: {type=EXTERNAL}

{type=EXTERNAL, classType=Validation@3feba861}

Comments

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.