permaculture playing cards*
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 Murach's Java Servlets and JSP this week in the Servlets 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

tranfer map values from one to another

sapna rana

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


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

Joined: Oct 13, 2005
Posts: 37900
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

Joined: Oct 27, 2005
Posts: 19649

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.

How To Ask Questions How To Answer Questions
Campbell Ritchie

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

Joined: Oct 27, 2005
Posts: 19649

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).
wood burning stoves
subject: tranfer map values from one to another
Similar Threads
hash map error
problem with struts2 drop down
Iterating over Map using JSTL
create property fiel with Map
HashMap, DropDown List, order of values