Difference between Array List and Linked List
Before we get into the actual differences, let us get to know what they really are?
What is a Set?
We have come across the concept of Set in mathematics and the Set here more or less means the same. Yes, it is a collection of elements and in most cases, a collection of similar elements. You can try adding these elements to a Set and try printing to understand, how it is actually stored.
Input to a Set: 20, 50, 10, 30.
It is actually stored in a Set as 10, 20, 30, 50.
The elements are sorted here and are stored not in their insertion order. This is one of the features of Set that it always sorts the elements before storing and of course, there are exceptions to it and one such is the LinkedhashSet as it maintains the insertion order of the elements.
In computer terms, the Set includes few additional properties such as Methods and Inheritance. Methods are just like functions and they do certain tasks such as adding, removing or iterating through the set of elements. Most of us are well-known of the term Inheritance and it means the same here. Yes, we can inherit a Method from its Collection, so that it can be used with the Set Interface. Again we are talking about a new term i.e. the Set Interface, and it is nothing more than the whole set of elements including the Methods.
How to implement it?
Just for a better understanding, we have represented a Set with its Syntax. From the below syntax, you could identify the different types of Set such as the HashSet and TreeSet.
import java.util.*;
public class Setexample {
public static void main(String args[]) {
int count[] = {34, 22,10,60,30,22}{25, 17, 76, 12, 88, 57};
Set<Integer> set = new HashSet<Integer>();
try {
for(int i = 0; i < 5; i++) {
set.add(count[i]);
}
System.out.println(set);
TreeSet sortedSeteg = new TreeSet<Integer>(set);
System.out.println(“Here we have the sorted output:”);
System.out.println(sortedSeteg);
System.out.println(“Look at the First element: “+ (Integer)sortedSet.first());
System.out.println(“Look at the last element: “+ (Integer)sortedSet.last());
}
catch(Exception e) {}
}
}
The output of the above code is as follows.
[25, 17, 76, 12, 88]
Here we have the sorted output:
[12, 17, 25, 76, 88]
Look at the First element: 12
Look at the last element: 88
What is List?
A List extends Collection similar to how a Set did, but it maintains the order of insertion. You try adding the following names to a list and see how is added to it.
The input to a List: John, Nancy, Mary, Alice.
How it is stored in a List: John, Nancy, Mary, Alice.
Just notice the order in which they are inserted. You can identify that ‘John’ being the first element in the input as well as output and is followed by the same order in which the names are inserted. We can even consider this one of the major properties of List.
How to implement it?
Let us look at few of the methods of List such as ArrayList and LinkedList in the below syntax.
import java.util.*;
public class Collectionssample {public static void main(String[] args) {
List a1 = new ArrayList();
a1.add(“John”);
a1.add(“Nancy”);
a1.add(“Mary”);
a1.add(“Alice”);
System.out.println(” ArrayList Elements are”);
System.out.print(“\\t” + a1);List l1 = new LinkedList();
l1.add(“Silvia”);
l1.add(“Arjun”);
l1.add(“Deepika”);
l1.add(“Susan”);
System.out.println();
System.out.println(” LinkedList Elements are”);
System.out.print(“\\t” + l1); }}
The output of the above syntax is as follows.
ArrayList Elements are
[John, Nancy, Mary, Alice]
LinkedList Elements
[Silvia, Arjun, Deepika, Susan]
It is very clear from the above code that both the ArrayList and the LinkedList maintains the insertion order.
How they Differ?
- Methods and Descriptions:
The Set and the List has its own methods and let us look at few of them here.
S.No | Set – Methods | List – Methods |
1. | add() – It is to add objects to a collection. | void add(int index, Object obj) – It adds the object ‘obj’ at the specified ‘index‘ of the invoking list and it makes sure that no element is overwritten by shifting the previous elements. |
2. | clear() – It is to remove objects from a collection. | boolean addAll(int index, Collection c) – It adds the entire collection ‘c’ to the invoking list and at the ‘index’ specified. It also ensures that no elements are overwritten. We can also check the correctness of its operation by examining the return value. It returns ‘true’ if the change is successful otherwise, it returns a value ‘false’. |
3. | contains() – It is to check whether the Set contains a certain object in it. It returns a value ‘true’ if the object is present in the Set. | Object get(int index) – It returns the element or object at the specified ‘index’. |
4. | isEmpty() – It is to determine whether the collection helps in as no elements in it. It returns a value ‘true’ if there is no element. | int lastIndexOf(Object obj) – It works similar to the reverse of the indexOf() Method. It returns the last occurrence of the specified Object ‘obj’ and a value ‘1’ is returned if there of no such object in the list. Therefore, it can also be used as contains() Method of the Set Interface. |
6. | remove() – It is to remove an element from a collection by specifying it as a parameter to the method. | ListIterator listIterator() – It returns an iterator to the starting index of the List. |
7. | size() – It is to count the number of objects or elements that a collection has. | ListIterator listIterator(int index) – It helps in iterating through the invoking List starting at the specified ‘index’. |
8. | – | Object remove(int index) – It deletes the object at the specified ‘index’ and returns the deleted element as result. It also decrements the resultant list indices to reflect the deletion. |
9. | – | Object set(int index, Object obj) – It is to assign the Object ‘obj’ to the invoking list at the specified ‘index’. |
10. | – | List subList(int start, int end) – It is to include the objects from the index ‘start’ to the index ‘end’ in the list that has invoked the Method. |
- Insertion Order maintenance:
The Set never maintains the order of the elements in which they are inserted into it whereas the List does maintain it. There is an exception to this rule for LinkedHashSet as it maintains the order of insertion but the other Set such as HashSet and TreeSet sorts the elements before storing it. The same is described with examples below.
Set Input: Cat, Doll, Apple.
Stored as: Apple, Cat, Doll.
List Input: Cat, Doll, Apple.
Stored as: Cat, Doll, Apple.
- The presence of Duplicates:
A Set never allows duplicates whereas a List allows it. If a duplicate value has to be added to a list then it gets overwritten. Look at the samples for Set and List duplicates.
Set Input: 10, 20, 20, 50.
Stored as: 10, 20, 50.
List Input: 10, 20, 20, 50.
Stored as: 10, 20, 20, 50.
- Null Values:
A Set can have only one null value whereas a List can have more than one null value and is not restricted to any number.
Set Input: null, null, Man, Dog, Plane.
Stored as: null, Dog, Man, Plane.
List Input: null, null, Man, Dog, Plane.
Stored as: null, null, Man, Dog, Plane.
- Use of Iterator & ListIterator:
The Iterator method works well with both Set and List whereas the method ListIterator works only with List. The ListIterator can be used to traverse forward as well as backward through the List.
- The presence of Legacy Class:
The Set has no legacy class whereas the List interface has a legacy called as the ‘vector’. A vector uses the List interface and therefore it maintains the order of insertion. Due to the synchronization efforts, the performance of vector in additions, deletions & updates is little slower.
- Implementations:
Few of the Set implementations are HashSet, LinkedHashSet, and TreeSet. Few of the implementations of List include ArrayList and LinkedList.
When to use Set & List?
The usage of Set and List is purely dependent on the requirement of maintenance of the insertion order. As we have learned that a Set never maintains the order of insertion, it can be used when the order is of lesser importance. In a similar way, use the List when you need to maintain the sequence of insertion as well.
Differences in a tabular form:
S.No | Differences in | Set | List |
1. | Insertion Order | It maintains the insertion order. The first inserted remains in the first place and so on irrespective of its value. | It never maintains the insertion order. |
2. | Methods | It uses the methods such as add(), clear(), contains(), isEmpty(), remove() and size(). | It uses the methods such as add(), addAll(), get(), lastIndexOf(), ListIterator() with or without parameter, remove(), set(), and subList(). |
3. | Duplicates | It never allows duplicates and in the case of such appearances, the value gets overwritten. | It allows duplicates. |
4. | Null values | It can have only one null value at the maximum. | It can have any number of null values. |
5. | Use of Iterator() & listIterator() | It uses only the method iterator(). | It uses both the iterator() as well as the listIterator(). |
6. | Presence of Legacy Class | There is no Legacy class. | It has as Legacy class called as a vector. |
7. | Implementations | Few of the Set interface implementations are HashSet, LinkedHashSet and Tree Set. | Few of the List interface implementations are LinkedList and ArrayList. |
Hope, we have included every possible difference between the Set and the List. If you feel that we have missed something, please do let us know.