Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing hashmaps  RSS feed

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi GUys

I have the following hasmap in my servlet:

long mailId = 1;
List<Class> recipients //etc
HashMap<long, Contact> hash = new HashMap<long, Contact>();
hash.put(mailId, recipients);

What I want to do on my JSP page is if the mailId matches a preset number the code will get the recipients array and print it out. How can I do this ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where's your attempt at it?

And please, please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok - I usually use them not I didnt do it this time. I havent tried because I really don't know where to begin. I need a if statement to compare the values and then if they match, get the recipients list and iterate though it. The iteration isn't the problem here, its how I can go through the hashmap (since Ive never used it before). I did google but no decent results.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know you need an if, and you know you need a loop for iteration, then you know that you at least need the <c:if> and <c:forEach> actions. That's the place to start.
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what i have:



Doesnt work though..
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the HashMap class doesn't have a getRecipients() method, so ${hash.recipients} isn't going to return anything. My guess is that what you actually are after is the Contact object which belongs to the key you just tested for. Is that right?

If so, the EL has syntax that allows you to access Map objects directly via their keys. Just off-hand I don't remember the syntax but I encourage you to look that feature up.

Edit: I had another look at your original post and whereas the values of your HashMap are supposed to be Contact objects, your code actually tries to add a List<Class> object. It would be better for you and for us if we were looking at the real code.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: It would be better for you and for us if we were looking at the real code.

Indeed, the more difficult you make it for people to help you, the less likely you will be to receive help.

With regards to:
Is this valid EL syntax? Where's the ${} notation? You also need to grab a copy of the JSP or EL specification to learn what you can and cannot do in the EL. Calling general methods is one of the things that you cannot.

Now, given that you can look up a map entry easily using its key, you can test if the entry exists with:
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is the code:



The reason I have done it like this is because I have to set the recipeints for each mail record. So to place an list of recipient objects into a hash along with the key as mail id so that as I iterate through the mail objects I can get all the recipients using an inner loop.
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that code shouldn't even compile. You're trying to use a List<Contact> object in a place where a Contact object is required. I would suggest getting your data structures straightened out before you try to use them. Otherwise there's just going to be confusion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!