• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

How to get session object into a hashmap

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am saving a HashMap into a session like so:

   

But how am I supposed to get it out of the session back into a HashMap ?

I am aware that the session data is stored as an object but do not know how to get it into a `HashMap`

I've tried:

   

But it says

cannot from Object to HashMap

 
Saloon Keeper
Posts: 5775
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to cast it to the correct type, like this:

(HashMap<Integer, Cart>) session.getAttribute("cart")
 
Vojislav Kovacevic
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any way to get rid of unchecked warning when I do this?

I am using @SuppressWarnings("unchecked"), is there a better option in your opinion?
 
Tim Moores
Saloon Keeper
Posts: 5775
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, no. The servlet session API deals only in Objects.
 
Saloon Keeper
Posts: 10531
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a natural consequence of session attributes not being type-safe. Suppressing the warning is fine if you know for sure that your application always assigns the same type to that attribute.

What's more problematic is your naming. It looks like your Cart class encapsulates a single entry of a shopping cart. So why is it called Cart and not something like CartEntry?

If you write a separate Cart class that encapsulates a Map<String, CartEntry> (after all, why are IDs represented by int? Are you going to use arithmetic on them?) you can cast the attribute to Cart and you won't get a warning:
 
Vojislav Kovacevic
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephen,

It's just the logic I went with on the fly.

The ids are there as keys to separate each product (which is what the Cart class contains).

I will think about what you said and try to implement it and see if it's a better solution.

I am always open to better code!
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!