2

I get requestParameters Map from my app and assigning it to a different map with some changed values. Basically output I get is

[email protected]
login
projectname=abc

I want to assign

[email protected]
request=login
projectname=abc

So i did this

    tempKey=new String[requestParameters.size()];
    tempValue=new String[requestParameters.size()];
    requestParams=new HashMap();

    while(iterator.hasNext())
    {

        Map.Entry me=(Map.Entry)iterator.next();
        String[] arr=(String[])me.getValue();

        if(me.getKey().toString().equalsIgnoreCase("login"))
        {
            tempKey[i]="request";
            tempValue[i]=me.getKey().toString();

        }
        else
        {
            tempValue[i]=arr[0];
            tempKey[i]=me.getKey().toString();
        }

        requestParams.put(tempKey[i], tempValue[i]);
        log.info(tempKey[i]+"="+tempValue[i]);
        i++;
    }

I try to print the values from requestParams like this, but i get nothing

iterator=requestParams.entrySet().iterator();
    while(iterator.hasNext())
    {

        Map.Entry me=(Map.Entry)iterator.next();
        String[] arr=(String[])me.getValue();
        log.info(me.getKey().toString()+"="+arr[0]);
    }

It correctly prints the log using tempKey[i]+"="+tempValue[i] but it does not assign values to requestParams (modified map), What is wrong in the above code?

3
  • Why are you using these tempValue and tempKey arrays?
    – Jon Skeet
    May 19, 2011 at 6:07
  • just ignore that, i was doing some other stuff with it, testing purpose
    – AabinGunz
    May 19, 2011 at 6:09
  • 1
    Rather than telling us to ignore things, why not fix the example? There's far too much that we're not seeing here - please change the question to show a short but complete example, saying what you're seeing and what you're expecting to see.
    – Jon Skeet
    May 19, 2011 at 6:14

2 Answers 2

3

I would copy the existing map and change the different values:

Map<String, String> newMap = new HashMap<String, String>(requestParameters);
newMap.put("request", "login");
newMap.remove("login");
0

the request parameters map, normally is an unmodifiable map .. therefore any changes you make to the make will not be saved. You need to create another map and put the all the entries in your map which you can change.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.