How to iterate over MultivaluedMap and extract the key and its corresponding first value?

One way to do this can be :--

  private Map<String,String> prepareParameters(MultivaluedMap<String, String> queryParameters) {

   Map<String,String> parameters = new HashMap<String,String>();

   Iterator<String> it = queryParameters.keySet().iterator();


         while(it.hasNext()){
           String theKey = (String)it.next();
           parameters.put(theKey,queryParameters.getFirst(theKey));
       }

   return parameters;

    }

OR without using the iterator, as suggested by Dinei Rockenbach, we can do as follow:-

  private Map<String,String> prepareParameters(MultivaluedMap<String, String> queryParameters) {

   Map<String,String> parameters = new HashMap<String,String>();

   for(String str : queryParameters.keySet()){
     parameters.put(str, queryParameters.getFirst(str));
   }
   return parameters;

    }

Or using streams...

queryParameters.getRequestHeaders().entrySet().stream() .collect( Collectors.toMap( Map.Entry::getKey, entry -> entry.getValue().stream().findFirst().orElse(null)));