Originally posted by bob morkos:
Is the statement above correct and should I use a List?
Originally posted by Joe Ess:
That statement is kind of like saying "Should I drive a car, or a Toyota?". java.util.List extends java.util.Collection and they are both interfaces. java.util.Collections is a class, but it is made up of utility methods to be used on java.util.Collection instances. Maybe you should browse through the Java Tutorial Collections Trail
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
I'm told that the Collection approach is not the best and that I must use List, dut that if you deal with Collections instead of Lists, you lose all the benefits of static binding and compile time checking.
Uh... what?
If your application needs to be able to access elements in a particular reliable order, or if it needs to use a method like get(int), then make sure you use a List, because a generic Collection doesn't (necessarily) have this behavior. If you don't need any methods or guarantees which are specific to List, then you should probably use a Collection. Because later on you may decide that a Set of some sort is more appropriate, and using Collection lets your code be as flexible as possible.
Originally posted by bob morkos:
I think if you use a Collection than you can cast it to which ever objects being an ArrayList, List, Vector, ..... This let's the developer to use his preferred approach being List, ArrayList, Vector, ..... Is that statement correct?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
Without posting a response to the original question (as this may produce even further confusion), on the topic of using a List (or interface type) reference, the suggestion of casting to a concrete implementation is just as bad (if not worse) as declaring a concrete type reference.
ArrayList a = new ArrayList(); // BAD
List list = new ArrayList(); // GOOD
List list = new ArrayList();
blah = (ArrayList)list; // BAD
The need to downcast highlights a design flaw (or perhaps a misunderstanding of the benefits of using an interface reference type).
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Don't get me started about those stupid light bulbs. |