• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap reference

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i reuse s hashmap within a loop without loosing the value..

If i have a code something like............

HashMap outerMap = new HashMap();
while(//some condition)
{
HashMap temp = new HashMap();
temp.put(key, value);
outerMap.putAll(temp);
}
someFunction(outerMap);


Here temp is used again in the iteration. If i dont use clear() function, then all the previous key/value pairs will be maintained in temp. But if i use clear(), then the value in outerMap will also be lost. Is there anyway for tackling this??
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you need the temp Map at all? Why not simply write?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akhil Vadekk:
Here temp is used again in the iteration. If i dont use clear() function, then all the previous key/value pairs will be maintained in temp.

No, the values are all lost because you are declaring the local variable anew.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic