开发者

How to assign values to a Map in java

开发者 https://www.devze.com 2023-03-07 03:04 出处:网络
I get requestParameters Map from my app and assigning it to a different map with some changed values. Basically output I get is

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

email=a@a.com
login
projectname=abc

I want to assign

email=a@a.com
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();
        }

开发者_如何学Python        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?


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");


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消