• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Using forEach to display values from a Map

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?




 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you do?

 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks a lot.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic