8/25/2004

What should methods return arrays or Collections?

To prolong Paul River's Weblog: By returning arrays we have a temporary advantage of typed objects(untill we java 5). There are many other advantages if a method returns Collections, We can decorate collections as needed.
  • We can implement lazy loading returned objects very easily (with arrays also it may be possible by proxies etc in a long way).
  • We can easily return (private) inner data (unmodifiable), instead of returning new cloned array each time.
  • We can share data between multiple threads.
  • Collection is as simple to use as an array type.

In summary returned collection can be changed to any sub type collection (which is very very useful from implementors perspective) with minimal client changes.


98 Comments:

At 8/30/2004, Anonymous Anonymous said...

We can hold collections inside and return arrays as needed. IMO it is simple.
May be arrays are easy to use in scripted java apps like JSPS

 
At 2/04/2005, Blogger Srini said...

In a library like Java it mandates to have a spec with specific types. I am not going with the same idea with in an application. A db layer returning collections is more easier to use than an array. At the most we get IllegalCastException, when underlying db layer changes independently. But that is reasonable damage when compared to changes in Java spec. Isn't it that we were mould to make interfaces as generic and stable as
possible (alowing implementations to change)?

 
At 11/02/2007, Anonymous Anonymous said...

dJlwNS The best blog you have!

 
At 11/02/2007, Anonymous Anonymous said...

3OPePe Magnific!

 
At 11/02/2007, Anonymous Anonymous said...

Wonderful blog.

 
At 11/02/2007, Anonymous Anonymous said...

Good job!

 
At 11/03/2007, Anonymous Anonymous said...

Hello all!

 
At 11/03/2007, Anonymous Anonymous said...

Wonderful blog.

 
At 11/03/2007, Anonymous Anonymous said...

Hello all!

 
At 11/03/2007, Anonymous Anonymous said...

Magnific!

 
At 11/03/2007, Anonymous Anonymous said...

Thanks to author.

 
At 11/04/2007, Anonymous Anonymous said...

5zgd9S write more, thanks.

 
At 11/04/2007, Anonymous Anonymous said...

Wonderful blog.

 
At 11/04/2007, Anonymous Anonymous said...

Wonderful blog.

 
At 11/04/2007, Anonymous Anonymous said...

Please write anything else!

 
At 11/04/2007, Anonymous Anonymous said...

Please write anything else!

 
At 11/04/2007, Anonymous Anonymous said...

Magnific!

 
At 11/05/2007, Anonymous Anonymous said...

Magnific!

 
At 11/05/2007, Anonymous Anonymous said...

Please write anything else!

 
At 11/05/2007, Anonymous Anonymous said...

Magnific!

 
At 11/05/2007, Anonymous Anonymous said...

Wonderful blog.

 
At 11/05/2007, Anonymous Anonymous said...

Thanks to author.

 
At 11/05/2007, Anonymous Anonymous said...

Please write anything else!

 
At 11/05/2007, Anonymous Anonymous said...

Good job!

 
At 11/05/2007, Anonymous Anonymous said...

Magnific!

 
At 11/05/2007, Anonymous Anonymous said...

Calvin, we will not have an anatomically correct snowman!

 
At 11/05/2007, Anonymous Anonymous said...

A flashlight is a case for holding dead batteries.

 
At 11/05/2007, Anonymous Anonymous said...

All generalizations are false, including this one.

 
At 11/05/2007, Anonymous Anonymous said...

Energizer Bunny Arrested! Charged with battery.

 
At 11/05/2007, Anonymous Anonymous said...

Nice Article.

 
At 11/05/2007, Anonymous Anonymous said...

Suicidal twin kills sister by mistake!

 
At 11/05/2007, Anonymous Anonymous said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

 
At 11/05/2007, Anonymous Anonymous said...

The gene pool could use a little chlorine.

 
At 11/05/2007, Anonymous Anonymous said...

Oops. My brain just hit a bad sector.

 
At 11/05/2007, Anonymous Anonymous said...

What is a free gift ? Aren't all gifts free?

 
At 11/05/2007, Anonymous Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

 
At 11/05/2007, Anonymous Anonymous said...

When there's a will, I want to be in it.

 
At 11/05/2007, Anonymous Anonymous said...

Ever notice how fast Windows runs? Neither did I.

 
At 11/05/2007, Anonymous Anonymous said...

Oops. My brain just hit a bad sector.

 
At 11/05/2007, Anonymous Anonymous said...

A lot of people mistake a short memory for a clear conscience.

 
At 11/05/2007, Anonymous Anonymous said...

Clap on! , Clap off! clap@#&$NO CARRIER

 
At 11/05/2007, Anonymous Anonymous said...

What is a free gift ? Aren't all gifts free?

 
At 11/05/2007, Anonymous Anonymous said...

Give me ambiguity or give me something else.

 
At 11/05/2007, Anonymous Anonymous said...

A flashlight is a case for holding dead batteries.

 
At 11/05/2007, Anonymous Anonymous said...

Suicidal twin kills sister by mistake!

 
At 11/05/2007, Anonymous Anonymous said...

I don't suffer from insanity. I enjoy every minute of it.

 
At 11/05/2007, Anonymous Anonymous said...

Beam me aboard, Scotty..... Sure. Will a 2x10 do?

 
At 11/05/2007, Anonymous Anonymous said...

I don't suffer from insanity. I enjoy every minute of it.

 
At 11/05/2007, Anonymous Anonymous said...

Ever notice how fast Windows runs? Neither did I.

 
At 11/05/2007, Anonymous Anonymous said...

Friends help you move. Real friends help you move bodies

 
At 11/05/2007, Anonymous Anonymous said...

Clap on! , Clap off! clap@#&$NO CARRIER

 
At 11/05/2007, Anonymous Anonymous said...

What is a free gift ? Aren't all gifts free?

 
At 11/05/2007, Anonymous Anonymous said...

Give me ambiguity or give me something else.

 
At 11/05/2007, Anonymous Anonymous said...

What is a free gift ? Aren't all gifts free?

 
At 11/05/2007, Anonymous Anonymous said...

Friends help you move. Real friends help you move bodies.

 
At 11/05/2007, Anonymous Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

 
At 11/05/2007, Anonymous Anonymous said...

Beam me aboard, Scotty..... Sure. Will a 2x10 do?

 
At 11/05/2007, Anonymous Anonymous said...

I'm not a complete idiot, some parts are missing!

 
At 11/05/2007, Anonymous Anonymous said...

640K ought to be enough for anybody. - Bill Gates 81

 
At 11/06/2007, Anonymous Anonymous said...

Calvin, we will not have an anatomically correct snowman!

 
At 11/06/2007, Anonymous Anonymous said...

Calvin, we will not have an anatomically correct snowman!

 
At 11/06/2007, Anonymous Anonymous said...

Thanks to author.

 
At 11/06/2007, Anonymous Anonymous said...

Thanks to author.

 
At 11/06/2007, Anonymous Anonymous said...

Beam me aboard, Scotty..... Sure. Will a 2x10 do?

 
At 11/06/2007, Anonymous Anonymous said...

Hello all!

 
At 11/06/2007, Anonymous Anonymous said...

What is a free gift ? Aren't all gifts free?

 
At 11/06/2007, Anonymous Anonymous said...

What is a free gift ? Aren't all gifts free?

 
At 11/06/2007, Anonymous Anonymous said...

If ignorance is bliss, you must be orgasmic.

 
At 11/06/2007, Anonymous Anonymous said...

Energizer Bunny Arrested! Charged with battery.

 
At 11/06/2007, Anonymous Anonymous said...

The gene pool could use a little chlorine.

 
At 11/06/2007, Anonymous Anonymous said...

640K ought to be enough for anybody. - Bill Gates 81

 
At 11/06/2007, Anonymous Anonymous said...

Lottery: A tax on people who are bad at math.

 
At 11/06/2007, Anonymous Anonymous said...

Ever notice how fast Windows runs? Neither did I.

 
At 11/06/2007, Anonymous Anonymous said...

Build a watch in 179 easy steps - by C. Forsberg.

 
At 11/06/2007, Anonymous Anonymous said...

A flashlight is a case for holding dead batteries.

 
At 11/06/2007, Anonymous Anonymous said...

Suicidal twin kills sister by mistake!

 
At 11/06/2007, Anonymous Anonymous said...

All generalizations are false, including this one.

 
At 12/18/2012, Anonymous Anonymous said...

Bad accept for gospel payday loans cosset acutely to you action as you look for a loan that matches your demands. [url=http://trustedpaydayloans.org.uk]payday loans lenders[/url] A Establishment advance of payday loans is abject absorb as admissible. Strolling Christian into a facility which arm sit down with you and be indicative of how the benefits of this loan.

 
At 1/23/2013, Anonymous Anonymous said...

viagra pills viagra 65 year old - order viagra online nz

 
At 1/24/2013, Anonymous Anonymous said...

generic viagra viagra online canadian pharmacy paypal - cheap viagra no prescription

 
At 2/04/2013, Anonymous Anonymous said...

purchase soma soma muscle relaxant abuse - soma drug real

 
At 2/04/2013, Anonymous Anonymous said...

buy soma online buy soma online with paypal - soma discount coupon codes

 
At 2/10/2013, Anonymous Anonymous said...

order tramadol tramadol 50mg yahoo - buy tramadol echeck

 
At 2/12/2013, Anonymous Anonymous said...

generic cialis cialis-72 saat - buy cialis online malaysia

 
At 2/12/2013, Anonymous Anonymous said...

cialis for sale should you buy cialis online - cialis online acquisto

 
At 2/13/2013, Anonymous Anonymous said...

generic cialis buy cialis in china - buy cialis online overnight

 
At 2/22/2013, Anonymous Anonymous said...

buy tramadol difference between tramadol for dogs and humans - buy tramadol online mastercard overnight

 
At 2/23/2013, Anonymous Anonymous said...

buy tramadol online no prescription buy tramadol online saturday delivery - tramadol 50 mg used for

 
At 2/24/2013, Anonymous Anonymous said...

cheap tramadol no prescription tramadol brand name ultram - can you buy tramadol over the counter in usa

 
At 2/26/2013, Anonymous Anonymous said...

buy tramadol tramadol overnight no prescription needed - tramadol hcl is it a narcotic

 
At 3/03/2013, Anonymous Anonymous said...

buy cialis online cheap generic cialis from usa - cheap viagra cialis levitra

 
At 3/06/2013, Anonymous Anonymous said...

learn how to buy tramdadol tramadol hcl is it a narcotic - tramadol 50mg side effects in dogs

 
At 3/09/2013, Anonymous Anonymous said...

http://buytramadolonlinecool.com/#61458 after effects tramadol overdose - tramadol 180

 
At 3/10/2013, Anonymous Anonymous said...

http://www.integrativeonc.org/adminsio/buyklonopinonline/#9183 klonopin for anxiety as needed - what does 2mg klonopin look like

 
At 3/10/2013, Anonymous Anonymous said...

buy tramadol online no prescription cheap tramadol hcl 50 mg information - can you buy tramadol over the counter usa

 
At 3/10/2013, Anonymous Anonymous said...

buy tramadol online tramadol 10mg for dogs - maximum dosage tramadol 50mg

 
At 3/11/2013, Anonymous Anonymous said...

buy tramadol cod online buy tramadol online utah - can you buy tramadol in greece

 
At 3/12/2013, Anonymous Anonymous said...

buy klonopin online cheap makers generic klonopin - klonopin side effects reviews

 

Post a Comment

<< Home