• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code I have the below casting

Set<FutGenInfo> futureSet = (LinkedHashSet<FutGenInfo>) model.get("abc");

Is there an issue doing this cast? What is wrong in this cast?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It makes no sense to cast to a specific implementation of Set (in this case, LinkedHashSet) if the variable you're going to assign the result to is not of that specific implementation type. Cast to the interface type Set instead of LinkedHashSet:

Set<FutGenInfo> futureSet = (Set<FutGenInfo>) model.get("abc");

Ofcourse, heed the usual warnings when casting: avoid casting as much as possible; if model.get("abc") returns something that's not compatible with Set<FutGenInfo> you'll get a ClassCastException.
 
Abhishek Rath
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is there in the link you posted?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no point in casting to LinkedHashSet if you are going to store it in a Set variable.
What type does model.get() return?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek Rath wrote:what is there in the link you posted?


Do you mean the "I agree. Here's the link" thing at the bottom? That is just an advertisement, I didn't post that (it's added by the forum software automatically) and it has nothing to do with your question or my answer.
 
Abhishek Rath
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me explain

In the data access layer I am doing

Set<FutGenInfo> futureGenScheduleList = new LinkedHashSet<FutGenInfo>();

Then populating thios from the query. The reason for using LinkedHashSet is to maintain the insertion order and my query is giving me the record in aparticular order. This object is paased to the controller and added to Model which is defined as below

Map<String, Object> model = new HashMap<String, Object>();

I am doing

model.put("abc", futureGenScheduleSet);

Now while getting it I am doing Set<FutGenInfo> futureSet = (LinkedHashSet<FutGenInfo>) model.get("abc");


 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, Paweł and I already explained why it doesn't make sense to cast it to LinkedHashSet.

Were our explanations clear, or do you still have a question about that?
 
Abhishek Rath
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is helpful. Thanks a lot.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic