1

Is there any method to convert the map obtained from servletRequest.getParameterMap() which returns a Map<String, String[]> to Map<String, Object[]> in a simple way other than a for loop?

I have a method getSomething() which is declared as

public Response getSomething(final Map<String, Object[]) 

I have to call this method with servletRequest.getParameterMap() as input.

3
  • 4
    Your question looks like XY problem. Can you add more info about why you need this change? Commented Jul 16, 2015 at 19:42
  • 1
    Also what do you mean by convert? Can we assume that you want to get new independent map which will be copy of original one (since otherwise you could try adding Integers to array of Objects which really will be still array for Strings). Commented Jul 16, 2015 at 19:56
  • 1
    Just in case you don't know it: to add more informations to your question use edit option. Commented Jul 16, 2015 at 19:59

2 Answers 2

7

Sure.

Map<String, Object[]> objectMap = 
    Collections.unmodifiableMap(servletRequest.getParameterMap());

This is correct, type-safe, and doesn't risk problems with accidentally putting wrongly typed values into the map.

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

1 Comment

I also doesn't prevent us from trying to place non-string value to String[] array via Object[] reference (although this behaviour will be stopped at runtime).
0

This way uses Java raw types.

Map<String, String[]> map1 = new HashMap<>();
Map<String, Object[]> map2 = (Map)map1;

This is a working solution although, as Pshemo pointed out, java raw types shouldn't be used.

1 Comment

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.