File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tranfer map values from one to another

 
sapna rana
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47231
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two Maps? I hadn't noticed that.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic