Having worked with hundreds of variables in my Xpages application, I was struck with the thought that there should be some way of reducing this number without losing out on their functionality. Suddenly, it struck me that the Hashmap could be the perfect solution to my problem.

Assume that you have an xpage where the values of three edit boxes should be “Hello”, “Welcome to”,”Maarga”. We normally would use the following code on the afterPage Load event of the XPage.

viewScope.put(“val2”,”Welcome to “)

and onn the XPage, we would call the following code in each of the edit boxes:

EditBox 1–>viewScope.get(“val1”)
EditBox 2–>viewScope.get(“val2”)
EditBox 3–>viewScope.get(“val3”)

This works fine as there are only 3 scoped variables used. But, as the number of scoped variables increases, the performance will deteriorate. Optimization of code becomes necessary and this is where Hashmap comes to the rescue. In our example we had used 3 scoped variables :val1,val2,val3. Instead we can use a single scoped variable and map multiple values to it .

Consider the following code:

var testHashMap:java.util.HashMap = new java.util.HashMap();
var array=[“Hello”,”Welcome to”,”Maarga”];
var values=”val”+(d+1);

This is similar to the following format got by using JSON object:

viewScope.put(“vwScopeVar”, {val1:”Hello”, val2:”Welcome to”, val3:”Maarga”});
To access the values on the XPage:

viewScope.vwScopeVar.val1 can be used in EditBox1,
viewScope.vwScopeVar.val2 can be used in EditBox2 and
viewScope.vwScopeVar.val3 can be used in EditBox3.

This speeds up the performance and allows us to minimize on the number of scoped variables.