What are the fundamental differences between the two objects? Is one more efficient? Does one have more methods?
The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.
ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.
The main difference between List and Set is that List allows duplicates while Set doesn't allow duplicates. List is an ordered collection it maintains the insertion order, which means upon displaying the list content it will display the elements in the same order in which they got inserted into the list.
List allows duplicates while Set doesn't allow duplicate elements . All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value. List permits any number of null values in its collection while Set permits only one null value in its collection.
List is in interface while ArrayList is a class.
See ArrayList, and List.
E.g, you can't use this setup:
List<String> list = new List<String>();
... Because it's an interface.
However, this works:
ArrayList<String> arrayList = new ArrayList<String>();
Also... You can do as duffymo says below, which is more or less the same as implementing the List
interface (making your own list implementation).
Consider a line like the following:
List<String> names = new ArrayList<String>();
If you're new to object-oriented architectures, you might have expected instead to see something like ArrayList<String> names = new ArrayList<String>();
. After all, you've just said that it's a new ArrayList
, so shouldn't you store it in a variable of type ArrayList
?
Well, you certainly can do that. However, List
is an interface--like a template of sorts--that ArrayList
is said to inherit. It is a contract that says "anytime you use a List
implementation, you can expect these methods to be available". In the case of List
, the methods are things like add
, get
, etc.
But ArrayList
is only one implementation of List
. There are others, such as LinkedList
. The two have the same interface, and can be used the same way, but work very differently behind the scenes. Where ArrayList
is "random" access, meaning that it directly finds a specific element of the array without iterating through the whole list, LinkedList
does have to start from the first element and go one-by-one until it gets to the element you need.
The thing is, while you do need to specify which you want when you create the object, you generally only need to communicate no more than the fact that it is a List
, so you simply say that's what it is. List
communicates that you have a collection that is intended to be in the order that it is given. If you don't need to communicate that much, you might consider passing it around as a Collection
, which is another interface (a super-interface of List
). Or, if all you need to communicate is that you can iterate over it, you might even call it an Iterable
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With