Share on Google+Share on Google+

Using of [] operator of EL with the Map

This page discusses - Using of [] operator of EL with the Map

Using [ ] operator of EL with the Map


EL expressions are always used within curly braces, and prefixed with a dollar($) sign and $(expression). The first named variable in the expression is either an implicit object or an attribute stored in one of the the four scopes i.e. page, request, session, or application. The [] operator is much more powerful than the dot, because it lets you access Map key or a bean property name as well as the access to arrays and Lists.

Map stores the values in the form of a key- value pair. Map can't have a duplicate keys, and each key is associated with at least one value. In map we can retrieve the value by using ${map["0"]}.

We have made one program on map by using the EL . We are retrieving the values of a map firstly by setting the map attribute and thereafter retrieving the value by using the key associated with the map.

The code of the program is given below:


<%@ page import = "java.util.*"%>
<% Map map = new HashMap();
map.put("name", "Tim");
map.put("address", "200,Saint Thomas Loop");
map.put("state", "Virginia, USA");
request.setAttribute("useMap", map);
<tr><td><b>The name is :</td></b> <td><b>${useMap[userName]}</tr></td></b><br>
<tr><td><b>The first element in Map is :</td></b> <td><b>${useMap["name"]}</tr></td></b><br>
<tr><td><b>The second element in Map is : </td></b> <td><b>${useMap["address"]}</tr></td></b><br>
<tr><td><b>The third element in Map is : </td></b> <td><b>${useMap["state"]}</tr></td></b><br>

The output of the program is given below:

Download this example.



Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Using of [] operator of EL with the Map  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image