views:

85

answers:

3

I have a question regarding struts.

I have a HashMap which has nearly 50 entries. Now I have to define this map inside an action class, say TestAction. As you know, this action class extends the Action class. Now my doubt is fundamental: Should I load the map as static? What are the advantages of loading this Map static? If I am going to call this Action class 100 times, would this map have been loaded only once?

Also, not every time of the action class call I may access this map. If I call this action class 100 times, only 40 times there may be a need to access this map.

If I load this map as

public Map getMap()
{
 Map testMap= new HashMap();
 testMap.put("Harish",25);
 testMap.put("Ravi",55);
 return testMap();
}

what's the disadvantage? Which is the best approach?

A: 

If the contents of the map will remain static, make it static - 50 entries is a tiny amount of data and will have negligible effect on the class load time.

Nick Holt
Ya it will be neglibible...But my only doubt is if the action class is called 100 times will the map be loaded once or 100 times?
Harish
@Harish: if you initialize the map in a static block the map will be initialized when the class is loaded, normally once.
Nick Holt
+2  A: 

If your data never changes, loading it statically seem the best option. It will be loaded only once.

      private static final Map testMap = new HashMap();
      static {
        testMap.put("Harish",25);
        testMap.put("Ravi",55);
      }


If you sometimes need to change it, other factors like testing, and threading, might cause other options to be preferable.

KLE
+1  A: 

The way you have it coded now - testMap will be re-created every time the getMap() method is called. I'm not sure what you mean by "if the action class is called" - if the getMap() method is called on your class, then yes - if some other method is called that doesn't call getMap() in some way, then no.

Should I load the map as static? What are the advantages of loading this Map static? If I am going to call this Action class 100 times this map would have been loaded only once?

If you make the Map reference static it will exist once at the class level. You can also statically load the Map, and this will only happen once. This should really only be done if you're not going to change the contents of the map, since you may run into threading problems or have to deal with figuring out when to empty the map if it's constantly getting data added to it but never removed, etc.

Also not every time of the action class call I may access this map.If I call this action class 100 times only 40 times there may be a need to access this map.

If you're worried that you may never need the Map, you may want to look into lazy loading - making the map a static reference, but always accessing it through a static method that checks if the Map's been created yet - and if not it gets created the first time it's needed rather than on startup. Though in this case it probably doesn't matter.

I have to define this map inside an action class

I think that's more of an assumption rather than a requirement - Since this is a web app - the better solution may be to put the Map into Application scope at startup - pretty much just like static, only it's not tied to a class.

You can add a ServletContextListener to listen for your web app to start and stop, and add the Map to Application scope by calling

servletContext.setAttribute("mapKey", testMap)

Then in your action (or anywhere else in your webapp) you can call

servletContent.getAttribute("mapKey")

and get access you your Map.

Nate