This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Set that only Items can be added Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Set that only Items can be added" Watch "Set that only Items can be added" New topic
Author

Set that only Items can be added

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Create a Set such that only Items can be added. Prove it.

I tried to do it Generic but as I expected a compile time error is thrown.
Is there another way of doing or it is just what the book is aking?



Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Java is strongly typed language and the compiler error is a testament to that.

You could also try this, just for learning's sake - Now that your Set can only accept Items and since any class in Java automatically is a subclass of Object, you could use type casting and introduce an instance of Object into the Set. See what happens then.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set that only Items can be added
 
Similar Threads
Generics doubt
EL Mystery
Question reg. Adding elements to HashSet / TreeSet
how to easily sort the same TreeSet few times?
TreeSet Question