How to request.getParameterNames into List of strings?

Just construct a new ArrayList wrapping the keyset of the request parameter map.

List<String> parameterNames = new ArrayList<String>(request.getParameterMap().keySet());
// ...

I only wonder how it’s useful to have it as List<String>. A Set<String> would make much more sense as parameter names are supposed to be unique (a List can contain duplicate elements). A Set<String> is also exactly what the map’s keyset already represents.

Set<String> parameterNames = request.getParameterMap().keySet();
// ...

Or perhaps you don’t need it at all for the particular functional requirement for which you thought that massaging the parameter names into a List<String> would be the solution. Perhaps you actually intented to iterate over it in an enhanced loop, for example? That’s also perfectly possible on a Map.

for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
    String name = entry.getKey();
    String value = entry.getValue()[0];
    // ...

Leave a Comment