We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Iterate over Hashmap in JSP - Struts

110210
110210 asked
on
Medium Priority
4,114 Views
Last Modified: 2013-11-24

I have a Bean class(myTO - Value Object with getters and setters) which is stored in a LinkedHashMap (called masterMap).
myTO(value object) has a Hashmap inside it.

Finally I've placed the masterMap in the request scope.

Can any one suggest me how to iterate the myTO(which is inside masterMap) , along with corresponding HashMap inside myTo (in JSP-Struts)?

I was able to go one level. i.e, iterate the myTO inside masterMap. But couldnt itterate the Hashmap insde the myTO(have to itterate using a key which is stored in a string array)

Sorry, If the Question is confusing :(
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
sounds like you need the key, otherwise how can you access the map

I have done something similar (I think).  

Let me understand exactly what you are doing...

you have a LinkedHashMap called masterMap, which holds a collection of MyTO objects.  These objects also contain a HashMap which you want to iterate over?  If that is correct you can try the following:

1) retrieve the masterMap from the request.
2) retrieve the set of keys from masterMap using masterMap.keySet();
3) iterate through the keys retrieving the myTO object.
4) for each myTO object, retrieve the HashMap.
5) again, use the keySet method of HashMap to get a Set of the keys.
6) iterate through the keys to retrieve the values/object associated.

example code:

LinkedHashMap<String, myTO> masterMap = (LinkedHashMap<String, myTo>)request.getAttribute("masterMap");

Set<String> keysForMasterMap = masterMap.keySet();
Iterator<String> keysIterator = keysForMasterMap.iterator();

while(keysIterator.hasNext()){

String key = keysIterator.next();
MyTO myTO = masterMap.get(key);

HashMap<String, XXXX> mapFromMyTO = myTO.getXXXX();

Set<String> keysFromMyTOMap = mapFromMyTO.keySet();
Iterator<String> myTOMapKeysIterator = keysFromMyTOMap.iterator();

while(myTOMapKeysIterator.hasNext()){
String keyForObject = myTOMapKeysIterator.next();
ObjectXXXX objectRetrieved = mapFromMyTO.get(keyForObject);

//Now you can do something with the object you have retrieved.

}

}

Author

Commented:
Phoenix101- I want to do it in JSP, using <logic:..> tags and not in Java.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.