We are learning about the Collection Interface and I was wondering if you all have any good advice for it\'s general use? What can you do with an Collection that you cannot
It's easy if you think of it like this: Collections are better than object arrays in basically every way imaginable.
You should prefer List
over Foo[]
whenever possible. Consider:
ArrayStoreException
can result at runtime.List>
or List>
). With an array you get compilation warnings and confusing runtime exceptions.equals
, hashCode
and toString
methods do what users expect; those methods on an array do anything but what you expect -- a common source of bugs.Object arrays will never be first-class citizens in Java.
A few of the reasons above are covered in much greater detail in Effective Java, Second Edition, starting at page 119.
So, why would you ever use object arrays?
List
as close to that API as you can