Re: Revisit: List list = new ArrayList();



Knute Johnson schreef:
Patricia Shanahan wrote:
Knute Johnson wrote:
...
And why would you not;

Collection c = new ArrayList();

ArrayList seems an unfortunate choice of implementing class, given the
code only needs the common features of Collection. ArrayList makes some
insertions and removals expensive for the sake of efficient indexed
access. Why pay that cost if you don't need indexed access?

Collection does not make it clear whether duplicates are allowed or not,
so I would usually use either List or Set instead.

Patricia

From the docs:

Collection

The JDK does not provide any direct implementations of this interface:
it provides implementations of more specific subinterfaces like Set and
List. This interface is typically used to pass collections around and
manipulate them where maximum generality is desired.

So wouldn't that suggest that Collection should be used instead of List?

It depends on what you want to do. If you use a data structure
somewhere, you will know whether you want to allow duplicates or not,
and whether you want random access etc.

Collection OTOH is to be used as argument to functions which do not care
about that. Browse around in the Collection API, you’ll get a grasp of
it. E.g. addAll() takes a Collection as argument: the class on which it
is called will take care of the duplicates-or-not-issue, that does not
depend on which kind of collection is passed to the function.
sort() OTOH will take a List as argument since order is needed to be
able to talk about sorting.

So generally, you’ll do:

List<Whatever> myList = new ArrayList<Whatever>();
Collection<Something> = functionReturningCollection; // note that it
might actually return a Set or List, or some custom implementation
public usefulMethod(Collection<Stuff> args) {
// I just want some Stuff instances, don’t care which ones, since I
only use the enhanced for loop
}

etc.

HTH, H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Attachment: signature.asc
Description: OpenPGP digital signature



Relevant Pages

  • Re: Tough (for me) regex case
    ... I had *no idea* that there were different implementations of "regular ... But I simply don't care about that. ... newsgroup are generally more interested in the computing problem than ... in some kind of whizzing match between two competing ideologies. ...
    (comp.lang.perl.misc)
  • Re: Ranges and (non)static constraints
    ... care if an integer would fit a machine word? ... some implementations fail at compile time, ...
    (comp.lang.ada)
  • Re: size of object
    ... >> For most implementations, the size of an object is the ... >> members, plus one word for the vtable, plus one word for ... >> But I still want to know why you care. ... What's with the arogant attitude, ...
    (comp.lang.cpp)
  • RE: Compare/Hide Dupliactes based on one field
    ... Since you don't care which director shows up, use the MAX function on that ... would like my query to find all directors, but if it finds two directors from ... I have these fields in the query which contains duplicates (Please ... Everything I have looked at before, will either find duplicates where the ...
    (microsoft.public.access.queries)
  • Re: Stack growth direction to thwart buffer overflow attacks
    ... >> Frankly i do not care about 'C99' since they don't deliver any software. ... You are complaining about ISO/ANSI standards beeing fuzzy and mayebe even wrong ... Then the code should be written with broken implementations in mind ... like ISO did with their ISO campain during the 90's. ...
    (comp.security.misc)