Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: return a Co...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 11 Topic 16078 of 16590
Post > Topic >>

Re: return a Collection (Set or List)

by Mark Space <markspace@[EMAIL PROTECTED] > May 13, 2008 at 11:37 AM

Roedy Green wrote:
> On Tue, 13 May 2008 11:29:57 GMT, thufir <hawat.thufir@[EMAIL PROTECTED]
>
> wrote, quoted or indirectly quoted someone who said :
> 
>> found   : java.util.Collection<a00720398.data.Guest>
>> required: java.util.List<a00720398.data.Guest>
>>        guests = console.scanFile(new File("Guests.txt"), new
GuestFactory
>> ());
>>                                 ^
> 
> All Lists are Collections but not all Collections are Lists.  Java
> insists on an explicit cast when the conversion might conceivably
> fail, e.g. Collection -> List.
> 
> You need to have your method return a List<Guest> or you need to cast
> the assignment with:  
> 
> guests  = (List <Guest>)xxxx;

This works, but is a little hackish.  As you say, sometimes you will 
return a Set.  The above will fail if you do return a set.

Consider substituting two methods for this one.  "getAsList" and 
"getAsSet" Then the caller can indicate which type is desired and always 
receive the correct type.

You can use built-in methods to transform Lists to Sets and vice versa. 
  It's usually just a couple of method calls.  Actually you could do 
this externally too and not have to touch your class.  Always get a 
list.  If you need a set, just transform the List into a Set.

You could always just:

    List myList
    myList = getSomeList();
    Set mySet = new SomeSet();
    mySet.addAll( myList );

This feels a bit safer to me than casting something that might fail.
 




 11 Posts in Topic:
return a Collection (Set or List)
thufir <hawat.thufir@[  2008-05-13 11:29:57 
Re: return a Collection (Set or List)
Lew <lew@[EMAIL PROTEC  2008-05-13 08:18:51 
Re: return a Collection (Set or List)
Roedy Green <see_websi  2008-05-13 12:40:58 
Re: return a Collection (Set or List)
Mark Space <markspace@  2008-05-13 11:37:53 
Re: return a Collection (Set or List)
Roedy Green <see_websi  2008-05-13 19:39:07 
Re: return a Collection (Set or List)
Eric Sosman <Eric.Sosm  2008-05-13 16:13:07 
Re: return a Collection (Set or List)
Lew <lew@[EMAIL PROTEC  2008-05-13 19:41:37 
Re: return a Collection (Set or List)
Roedy Green <see_websi  2008-05-14 08:22:59 
Re: return a Collection (Set or List)
thufir <hawat.thufir@[  2008-05-13 22:38:12 
Re: return a Collection (Set or List)
RedGrittyBrick <RedGri  2008-05-14 10:27:23 
Re: return a Collection (Set or List)
Lew <lew@[EMAIL PROTEC  2008-05-14 08:07:30 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 15:00:05 CST 2008.