• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it better to use a Collection or a List  RSS feed

 
bob morkos
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you deal with Collections instead of Lists, you lose all the benefits of static binding and compile time checking. Don't do it. List is an interface, not a concrete implementation, so it's good to use it.

Is the statement above correct and should I use a List?
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bob morkos:
Is the statement above correct and should I use a List?

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
 
bob morkos
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. Don't do it. List is an interface, not a concrete implementation, so it's good to use it.

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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bob morkos
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
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.
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

ArrayList and Vector both implement List AND Collection. What's the problem again?

you lose all the benefits of static binding and compile time checking.

Oh, yea. Poke whoever told you this in the eye. Find me somewhere in Java that isn't type-checked at compile time. That's one of the major features of the language.

I think if you use a Collection than you can cast it to which ever objects being an ArrayList, List, Vector, .....

Create an ArrayList, cast to a Collection then cast to a Vector and you will get a ClassCastException. Casting does not change what an Object is. You can only cast an Object to it's own type, the interfaces it implements and the objects it inherits from. Furthermore, casting is inefficient and not exactly Object Oriented Design.
Again: Read the tutorial. Maybe start from the beginning
[ December 15, 2003: Message edited by: Joe Ess ]
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a weird post. I never heard of such statement before. I got more confused as I read the follow ups. List simply extends Collection interface, and it's an ordered collection. Why is it causing so much confusion? All collcetion package uses Object as parameters and return Objects. Where is this static binding thing?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, this has gone a bit strange. It's easier than we seem to think.
You can declare parameters and variables and return values with Interface tyes. This is a good thing because it gives you flexibility to change actual types later. For example:
Collection myCollection = someClass.gimmeSomeStuff();
Now all I know is I have a Collection. SomeClass can give me a list or set or whatever meets its needs today. But I'm a bit limited, because I can only call methods defined by Collection. If I really need some fancier methods defined by List I have to do some work. I can do something like this:
List myList = new ArrayList(myCollection);
It's a good practice to use Interface types for variables, parameters, return types, etc. Keep that up!
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
bob morkos
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a better approach:
Collection list = new ArrayList(); // GOOD

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).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, the more abstract the better. You'll be able to handle more different types of objects without changing your code. But you can only go so abstract. As mentioned before, if you really need one of the methods of List, then a Collection variable will not do the job.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!