Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting  RSS feed

 
Abhishek Rath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is there in the link you posted?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 63
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is helpful. Thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!