Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using forEach to display values from a Map

 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,


I have a HashMap , say mapCat which has foll 2 entries:-

(11, Computer)
(22,Maths)
where 11 and 22 are the keys and Computer , Maths their respective values.


I added an attribute in the request scope for this map --

request.setAttribute("Categories" , mapCat);

In my JSP I want to display only the values present in mapCat using forEach so I wrote
this code -




But the above code displays bothe the key and the values :

11=Computer
22=Maths

I WANT TO DISPLAY ONLY THE VALUES . HOW TO DO IT ?




 
L. Wei
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you do?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simran Dass wrote:I WANT TO DISPLAY ONLY THE VALUES . HOW TO DO IT ?

Please: KeepItDown

David L. Wei wrote:Can you do?

Resort to scriptlets? Why regress to discredited technology?

The entries in a java.util.Map are instance of java.util.Map.Entry. That should give you the clue to solve the issue.
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear,

Foll is ok-

String str,int i;
Map<Integer,String> map = new HashMap<Integer,String>();

for( Map.Entry<Integer,String> me : map.entrySet() )
i = me.getValue();
str = me.getKey();
}

But using forEach and EL I don't know how to separate the key and value for
each entry. Sending a Collection (using map.values()) won't be a good idea.
Please help. I want a loop like above in my JSP and I don't want to use scriptlets.
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure about a HashMap but I have iterated through other objects and done something similar.

Try ${cary.value}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Golebiowski wrote:Try ${cary.value}

Sigh. This is what I was hoping Simran would be able to figure out on his own from looking at the javadoc for Map.Entry (which has a getValue() method).
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks. Both ${catry.key} and ${catry.value} . But I cannot understand how ?

Map.Entry is an interface with methods - getValue(),getKey(),setValue() then how come
${catry.key} and ${catry.value} work ? I know I am missing out on something very basic.
Looking forward for help. Thanks
 
Ankit Garg
Sheriff
Posts: 9578
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a Map with the values that you showed earlier i.e.

(11, Computer)
(22,Maths)

Then while iterating over the Map, during each iteration, cary will be an instance of Map.Entry. The key in that Map.Entry object during first iteration will be 11 and the value will be Computer. During the second iteration, the key will be 22 and the value will be Maths. Using ${cary.key} will print the key i.e. call the getKey method of Map.Entry instance and ${cary.value} will print the value i.e. call the getValue method of Map.Entry instance...
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit I understood that getKey() & getValue() will be called . But why?
value is property bec we have both getter and setter methods for value but
we have only the getKey() method , no setter for it. Will both key and value
be treated as properties and so are they being called ?
 
Ankit Garg
Sheriff
Posts: 9578
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A property doesn't necessarily need to have both getter and setter. If you are getting a property, then a getter is needed, whether there is a setter for the property or not doesn't matter...
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic