Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a List vs. an ArrayList? [duplicate]

Tags:

What are the fundamental differences between the two objects? Is one more efficient? Does one have more methods?

like image 243
user812892 Avatar asked Jul 29 '11 00:07

user812892


People also ask

Is there a difference between list and ArrayList?

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.

Does ArrayList have duplicates?

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.

Can list have duplicates in Java?

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.

Does list allow duplicate elements?

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.


2 Answers

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).

like image 56
whirlwin Avatar answered Sep 26 '22 11:09

whirlwin


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.

like image 40
Ray Avatar answered Sep 26 '22 11:09

Ray