aspose file tools*
The moose likes Beginning Java and the fly likes tranfer map values from one to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "tranfer map values from one to another" Watch "tranfer map values from one to another" New topic
Author

tranfer map values from one to another

sapna rana
Greenhorn

Joined: Sep 01, 2008
Posts: 18
Hi ,

I have a map which contains the list of all the product groups in an organisation and need to select some and transfer them to another drop down by the >> button and remove back the projects from the selected drop down by << button

layout is something like this

1st drop down Buttons (>> , << ) Secong drop down

so in order to get the results by clicking on >> button i will add option to the selected drop down and at the same time remove them from the default drop down . But next time when a user logs in we want that suppose their are by default 10 projects and user last time selected 2 product groups, then he will find only 8 projects in the default drop down . We can not change the value of default drop down , hence need to remove the selected values from the default map.

But i m not able to remove the values from the defalut map.
It gives me an error saying Concurrent Modification .Thereby i declare another map with the same values as the default map and removed the value from the secound map rather than the defaut map.

But i found that values get removed from both the map..
My code is as follows:

************************************

// code in order to set the product groups(default Product groups minus selected PGs)
Map pg = new HashMap();
pg =userForm.getProductGrps(); // proguctgroups
Map pg1 = new HashMap();
pg1 = userForm.getProductGrps();
Map spg= user.getSelectedtrHierarchy(); //selectedproductgroups
if(spg!=null && !spg.equals("") && spg.size()>0)
{
for (Iterator it=spg.keySet().iterator();it.hasNext(); )
{
Integer key = (Integer)it.next();
String value = (String)spg.get(key);
System.out.println("key "+key);
System.out.println("value "+value);
System.out.println("contains "+pg.containsValue(value));
for (Iterator itr=pg.keySet().iterator();itr.hasNext(); )
{
Object key1 = (Object)itr.next();
Object value1 =pg.get(key1);

if(value.equals(value1.toString()) || value == value1.toString())
{
System.out.println("inside if "+value1);
System.out.println("removed "+pg1.remove(key1));
}

}
}
System.out.println("product groups default size after"+pg1.size());
}

user.setProductGroups(pg1);
userForm.setUser(user);


**************************************
[edit]Change whitespace to avoid appearing. CR[/edit]
[ September 29, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38398
    
  23
Please use code tags and preserve the indentation; your code is very difficult to read.

Do you really have two for loops with Iterators nested inside each other? That is a surefire way to a ConcurrentModificationException if one Iterator modifies the Set structurally.

Why don't you go through the Map interface; there are probably ready-made methods to do everything you want.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18


Don't create pg as a HashMap which you will then throw away. Instead, just use


See previous comment.

spg will never equal "" since a Map can never equal a String. Remove that part.

Remove the == check since it will never be reached. If value == value1.toString() then value.equals(value1.toString()).


Campbell: the inner loop iterates over a different Map. I also doubt this can be avoided since Sapna needs to remove based on the value, not the key.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38398
    
  23
Two Maps? I hadn't noticed that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

There are even three of them: spg for the outer loop (selected product groups), pg for the inner loop (all product groups), and pg1 for removing (also all product groups).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tranfer map values from one to another