Javatpoint Logo
Javatpoint Logo

Difference Between List and Set in Java

In JDK 2.0, we used to use Vectors, Arrays, and Hashtable to group the objects into a single unit. In JDK 8, Collection framework come in to existence that provides several interfaces to work with a collection of data. List and Set interfaces are one of them that are used to group the object. Both interfaces extend the Collection interface. The main difference between List and Set is that Set is unordered and contains different elements, whereas the list is ordered and can contain the same elements in it. Let's discuss in detail.

List Interface

The java.util package provides the List interface for maintaining the ordered collection. A List can contain the null and duplicate values. The methods of the List are based on the index, so all the operations like insert, delete, update, and search is based on the index. ArrayList, LinkedList, Stack, and Vector are the implementation classes available in the List interface. In Java, we mostly used the ArrayList and LinkedList implementation classes to design a list.

Let's use the List interface in a Java program.

ListExample.java

Output:

Difference Between List and Set in Java

Set Interface

The Set interface belongs to the java.util package that extends the Collection interface. It restricts us from entering the distinct value in it. It stores the value in a sorted way, so it doesn't maintain the insertion order. The set interface is used to design the mathematical Set in Java.

Let's see an example to understand how to create a set in Java.

SetExample.java

Output:

Difference Between List and Set in Java

List Vs Set Interface

In Java, both the List and the Set are available in the Collection framework. In order to store the collection of objects as a single unit, Set and List interface are used. Apart from these similarities, both the interfaces have so many differences too, which are as follows:

S.No List Set
1. The list implementation allows us to add the same or duplicate elements. The set implementation doesn't allow us to add the same or duplicate elements.
2. The insertion order is maintained by the List. It doesn't maintain the insertion order of elements.
3. List allows us to add any number of null values. Set allows us to add at least one null value in it.
4. The List implementation classes are LinkedList and ArrayList. The Set implementation classes are TreeSet, HashSet and LinkedHashSet.
5. We can get the element of a specified index from the list using the get() method. We cannot find the element from the Set based on the index because it doesn't provide any get method().
6. It is used when we want to frequently access the elements by using the index. It is used when we want to design a collection of distinct elements.
7. The method of List interface listiterator() is used to iterate the List elements. The iterator is used when we need to iterate the Set elements.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA