You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by temp temp <mi...@yahoo.com> on 2006/03/23 17:51:04 UTC

[java.util.Map] How can I alter a java.util.Map while I am iterating it

    I have a  HashMap   with some data .I must alter  the Map by replacing the keys with their upper case.
       
         Map searchResultMap=           (Map)request.getAttribute("searchResults");
         ListIterator   searchResultMapIte=Arrays.asList(searchResultMap.keySet().toArray()).listIterator();
         while(searchResultMapIte.hasNext()){
                  Object  aKey=searchResultMapIte.next();
                  aKey=aKey.toString().toUpperCase();
                  searchResultMapIte.set(aKey);
         }          
      I was  trying to achieve this using ListIterator which provides set method .But after  the while loop when I print the 
      Map it is  the same Map without any change .My question is how can I   alter   a java.util.Map while I am iterating it.
      Thanks  & Regards
    
		
---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.

Re: [java.util.Map] How can I alter a java.util.Map while I am iterating it

Posted by vijay venkataraman <vi...@ltp.soft.net>.
Yes as said, you should not be doing this. Probably fix it at source.
But you can certaing look at this.
public class CapsKeyMap
{
    public static void main(String[] args)
    {
       
        Map<String, String> lcMap = new HashMap<String,String>();
        lcMap.put("one","ONE");
        lcMap.put("two","TWO");
        lcMap.put("three","THREE");
       
        System.out.println("bef " + lcMap);
       
        Set<String> keySet = lcMap.keySet();
        String[] it = keySet.toArray(new String[keySet.size()]);
       
        for(String key : it)
        {
            Object obj = lcMap.get(key);
            lcMap.remove(key);
            lcMap.put(key.toUpperCase(), (String)obj);
           
        }      
        System.out.println("Aft " + lcMap);      
    }
}

You need to take care of the types.

Thanks,
Vijay Venkataraman

Antonio Petrelli wrote:

> temp temp ha scritto:
>
>> I get this Map from a collection and collection might have 1000 or 
>> more  Maps so I first iterate over this  collection retrieve the map 
>> and  then iterate over this map  so creating a new Map for each  
>> iteration  is this a good design ?
>>   
>
>
> Surely it is not a good design, but that is up to you. You asked a 
> thing and I answered. Maybe you have to turn your question inside-out: 
> Why are you putting keys as non-uppercase strings, when you need it?
> Maybe you could implement a java.util.Map in a case-insensitive way, 
> where the key must be a string, and it does not matter what its keys 
> case is. But again this is up to you, I cannot do your homework :-P
> Ciao
> Antonio
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


------------------------------DISCLAIMER------------------------------
This message is for the named person's use only. It may contain 
confidential, proprietary or legally privileged information. No
confidentiality or privilege is waived or lost by any mistransmission. 

If you receive this message in error, please immediately delete it and 
all copies of it from your system, destroy any hard copies of it and 
notify the sender. You must not, directly or indirectly, use, disclose, 
distribute, print, or copy any part of this message if you are not the 
intended recipient. 

Lisle Technology Partners Pvt. Ltd. and any of its subsidiaries each 
reserve the right to monitor all e-mail communications through its 
networks. 

Any views expressed in this message are those of the 
individual sender, except where the message states otherwise and the 
sender is authorized to state them to be the views of any such entity.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [java.util.Map] How can I alter a java.util.Map while I am iterating it

Posted by Antonio Petrelli <br...@tariffenet.it>.
temp temp ha scritto:
> I get this Map from a collection and collection might have 1000 or more  Maps so I first iterate over this  collection retrieve the map and  then iterate over this map  so creating a new Map for each  iteration  is this a good design ?
>   

Surely it is not a good design, but that is up to you. You asked a thing 
and I answered. Maybe you have to turn your question inside-out: Why are 
you putting keys as non-uppercase strings, when you need it?
Maybe you could implement a java.util.Map in a case-insensitive way, 
where the key must be a string, and it does not matter what its keys 
case is. But again this is up to you, I cannot do your homework :-P
Ciao
Antonio


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [java.util.Map] How can I alter a java.util.Map while I am iterating it

Posted by temp temp <mi...@yahoo.com>.
I get this Map from a collection and collection might have 1000 or more  Maps so I first iterate over this  collection retrieve the map and  then iterate over this map  so creating a new Map for each  iteration  is this a good design ?
Thanks & Regards
  
Antonio Petrelli <br...@tariffenet.it> wrote:  temp temp ha scritto:
>     I have a  HashMap   with some data .I must alter  the Map by replacing the keys with their upper case.
Ok first of all you cannot modify a HashMap while you are iterating it. 
But your problem has a solution.
Iterate the map and store into another map the key-value pair (with the 
key turned to uppercase)
Then clear the original map and then call "putAll"


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



		
---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.

Re: [java.util.Map] How can I alter a java.util.Map while I am iterating it

Posted by Antonio Petrelli <br...@tariffenet.it>.
temp temp ha scritto:
>     I have a  HashMap   with some data .I must alter  the Map by replacing the keys with their upper case.
Ok first of all you cannot modify a HashMap while you are iterating it. 
But your problem has a solution.
Iterate the map and store into another map the key-value pair (with the 
key turned to uppercase)
Then clear the original map and then call "putAll"


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org